Redirect 在laravel中检查当前页面
我有一个数据库,它存储用户当前所在的页面,如果他们注销或被踢出,他们的位置将被存储为整数 我想做的是,当他们重新登录时,我希望他们重定向到他们的页面位置。此外,如果他们试图转到第8页,而他们只完成了第4页,他们需要重定向 我在PageScontoler构造函数和before过滤器中进行了尝试,这会导致重定向循环问题Redirect 在laravel中检查当前页面,redirect,laravel,routes,position,Redirect,Laravel,Routes,Position,我有一个数据库,它存储用户当前所在的页面,如果他们注销或被踢出,他们的位置将被存储为整数 我想做的是,当他们重新登录时,我希望他们重定向到他们的页面位置。此外,如果他们试图转到第8页,而他们只完成了第4页,他们需要重定向 我在PageScontoler构造函数和before过滤器中进行了尝试,这会导致重定向循环问题 App::before(function($request) { $position = DB::table('users')->whereId(Auth::user
App::before(function($request)
{
$position = DB::table('users')->whereId(Auth::user()->id)->pluck('position');
return Redirect::to('mypage');
});
我需要先检查一下位置,然后再重新定向。这应该在刀片中完成吗
“编辑我的路线”突出包装在before筛选器中
Route::group(array('before' => 'auth'), function()
{
Route::get('page1', array('as' => 'page1', 'uses' => 'PagesController@page1'));
Route::get('page2', array('as' => 'page2', 'uses' => 'PagesController@page2'));
Route::get('page3', array('as' => 'page3', 'uses' => 'PagesController@page3'));
Route::get('page4', array('as' => 'page4', 'uses' => 'PagesController@page4'));
Route::get('page5', array('as' => 'page5', 'uses' => 'PagesController@page5'));
});
通常,控制器只是创建页面,传递一些变量
public function page1() {
$data = array(
'title' => 'Page1',
'questions' => 'js/page1.js'
);
return View::make('page1')->with('data', $data);
}
您可以在应用程序::before filter中执行此操作。但在重定向之前,需要检查当前URL路径是否与要重定向到的路径不同。因为过滤器在每个请求上都运行,所以如果当前页面与要重定向到的页面相同,它将创建重定向循环。像这样的方法应该会奏效:
App::before(function($request)
{
if (Auth::check())
{
$position = DB::table('users')->whereId(Auth::user()->id)->pluck('position');
$path = 'page' . $position;
if ($request->path() !== $path)
{
return Redirect::route('page' . $position);
}
}
});
这还检查用户是否经过身份验证,因为它需要使用用户ID查询数据库。您能告诉我们您的路线吗?特别是控制位置的那一个。一个小问题错误未定义属性:Illumb\Http\Request::$path-我尝试使用\Illumb\Http\Request,但仍然不起作用。抱歉,我的错误是,$Request->path应该是$Request->path,因为path是一个方法,而不是属性。我会更新答案。