Laravel 5.2在设置受保护的重定向路径时使用命名路由


Laravel 5.2 using named route in setting protected redirectPath

我想在登录到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);
    }