What happened to Laravel's redirectTo() method?

后端 未结 4 851
悲哀的现实
悲哀的现实 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:48

    This is the redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php in Laravel v5.3.28

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

    In the later versions 5.3.29 and above. This was changed in file with commit:

    Add auth redirect path generation method (#16896)

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

    So this part of documentation applies for Laravel version 5.3.29 and later only

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

    protected function redirectTo() { // }


    Solution for v5.3.28

    To make redirectTo() method work in v5.3.28, manually add this in redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php.

    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }
    

提交回复
热议问题