What happened to Laravel's redirectTo() method?

后端 未结 4 848
悲哀的现实
悲哀的现实 2021-02-07 12:21

We can override this property to redirect users after login in LoginController:

protected $redirectTo = \'/home\';

And here is the statement fr

4条回答
  •  无人及你
    2021-02-07 12:51

    Simple solution

    Override redirectPath() instead of redirectTo().

    Using raw string return:

    protected function redirectPath()
    {
      if (Auth::user()->role==0) {
        return '/volunteer';
      } else {
        return '/donor';
      }
    }
    

    Or overriding redirectPath() to the Laravel 5.3.29 redirectPath() version and then your redirectTo() method will work.

    public function redirectPath()
    {
      if (method_exists($this, 'redirectTo')) {
       return $this->redirectTo();
      }     
      return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }
    

    Why redirectTo() is not working

    Tested overriding the redirectPath() or redirectTo() method in App\Http\Controllers\Auth\LoginController.php on a clean Laravel v.5.3.29 + default Auth, they work as expected.

    Example of redirectTo() method

    Documentation says:

    If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property.

    So, the function should look something like this:

    protected function redirectTo()
    {
        if(condition) {
          return "/your/path";
        } 
        return "/your/secondpath";
    }
    

提交回复
热议问题