Laravel 5登录后重定向回家

mas*_*ter 1 laravel laravel-5

我有一个页面,用户需要登录才能点击按钮,如果他们没有登录,他们会被带到登录页面进行登录.问题是登录后他们被重定向到主页.我希望将它们重定向回登录前的页面,但无法让它工作.

登录方法仍然是100%标准.我试过编辑这个功能,但没有运气.

public function postLogin(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email', 'password' => 'required',
    ]);

    $credentials = $request->only('email', 'password');

    if ($this->auth->attempt($credentials, $request->has('remember')))
    {
        return redirect()->intended($this->redirectPath());
    }

    return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => $this->getFailedLoginMessage(),
                ]);
}
Run Code Online (Sandbox Code Playgroud)

Mis*_*erP 5

如果您只是想将用户重定向到另一个页面而不是/ home,只需定义一个

protected $redirectTo = 'your-different-route';  // i.e. '/admin' 
Run Code Online (Sandbox Code Playgroud)

在AuthController的顶部

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = 'admin/';
    ......
Run Code Online (Sandbox Code Playgroud)

这应该覆盖默认路由.
无论如何,在你想要深入挖掘登录系统

return redirect()->intended($this->redirectPath());  
Run Code Online (Sandbox Code Playgroud)

是关键.查看intended方法(Illuminate\Routing\Redirector),你会发现这样的东西

    public function intended($default = '/', $status = 302, $headers = [], $secure = null)
    {
        $path = $this->session->pull('url.intended', $default);

        return $this->to($path, $status, $headers, $secure);
    }  
Run Code Online (Sandbox Code Playgroud)

然后,您将注入$this->redirectPath()定义为 的预期方法

public function redirectPath()
    {
        if (property_exists($this, 'redirectPath')) {
            return $this->redirectPath;
        }

        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }  
Run Code Online (Sandbox Code Playgroud)

基本上,每次用户执行登录时,系统都会检查是否有不同的intended路由,如果不是,则使用默认路由(/ home).您可以将默认重定向路由更改为非常redirectPath()方法,但请注意,这是laravel框架的一部分,因此每次laravel获取更新时,您可能会丢失更改.更安全的解决方案就像我上面提到的几行一样,覆盖redirect了AuthController中的内容(上传和内容不会影响控制器).

编辑
如果您想为每次登录设置自定义重定向路由,Laravel提供了一种方便的方法来实现开箱即用,并且,我们将再次使用该intended()方法.无论您将用户重定向到登录页面,让我们说getLogin()方法,您需要更改默认重定向,类似于

return view('auth.login');  
Run Code Online (Sandbox Code Playgroud)

return Redirect::guest('auth.login') //assuming that auth.login is your login view  
Run Code Online (Sandbox Code Playgroud)

通过这个简单的修复,您仍然将用户重定向到登录页面,但使用该guest()方法:

public function guest($path, $status = 302, $headers = array(), $secure = null)
{
    $this->session->put('url.intended', $this->generator->full());

    return $this->to($path, $status, $headers, $secure);
}  
Run Code Online (Sandbox Code Playgroud)

相当简单,但是,除了标准重定向之外to(),它在实际重定向之前设置为等于当前URL sessionintended变量,即您想要重定向到的页面.这将节省您的一天.
最后,在你的postLogin()方法中,只需设置

return Redirect::intended('default-route');  
Run Code Online (Sandbox Code Playgroud)

如果会话中未提供预期位置,则需要传递默认值.请注意,这只是一个安全加,因为默认情况下,您postLogin()已经拥有

redirect()->intended($this->redirectPath());  
Run Code Online (Sandbox Code Playgroud)

它默认使用redirectPath(),但现在Redirect::guest()应该提供一个intended值.