我想在登录到url为/dashboard/start
的页面后重定向
我的routes.php
包含以下路由:
Route::get('/dashboard/start', ['uses' => 'Settings'OrganisationController@index', 'as' => 'app.home']);
在Laravel Auth过程中,您可以通过在app'Http'Controllers'Auth'AuthController
控制器的开头添加一个声明来实现登录重定向覆盖,如下所示:
protected $redirectPath = '/dashboard/start';
我想在整个代码中使用命名路由,这样,如果我更改路由文件中的url,只要名称没有更改,它就不会影响代码。
我尝试过,但失败了:
protected $redirectPath = route('app.home');
我找不到一个例子,也没有提到这一点。有什么想法吗?谢谢
您有几个选项。您可以覆盖控制器上的redirectPath
方法。
protected $redirectPath = 'app.home';
public function redirectPath()
{
return route($this->redirectPath);
}
您也可以制作一个authenticated
方法。handleUserWasAuthenticated
方法(当Auth::attempt
成功时调用)检查authenticated
方法的存在,如果存在,它将调用它并返回结果,而不是正常的redirect()->intended($this->redirectPath())
。此方法将接收当前请求和经过身份验证的用户。
handleUserWasAuthenticated
如何调用authenticated
:
return $this->authenticated($request, Auth::user());
您可以通过多种方式重定向。你可以通过路由重定向。编写您的路线.php
Route::get('first-route',['uses'=>'TestController@first','as'=>'first']);
Route::get('second-route',['as'=>'second','uses'=>'TestController@second']);
并编写控制器
public function first(){
return "This is a simple Test Controller";
}
public function second(){
return redirect()->route('first');
}
你可以使用像这样的动作方法
public function second(){
return redirect()->action('TestController@first');
}
或
public function second(){
$url=action('TestController@first');
return redirect($url);
}