简体   繁体   English

注销后的Laravel 5重定向 - 如何重定向?

[英]Laravel 5 Redirect After Logout - How to Redirect Back?

I want to redirect back to where the user has been, after he logs out successfully, because I have methods that are accessible even if logged out. 我希望在他成功注销后重定向回用户所在的位置,因为即使已注销,我也可以访问这些方法。

I guard every method in my PhotosController except @show 除了@show,我保护我的PhotosController中的每个方法

public function __construct()
{
    $this->middleware('auth', ['except' => 'show']);
}

To set the redirect after logout I set the property in my AuthController like this: 要在注销后设置重定向,我在我的AuthController中设置属性,如下所示:

protected $redirectAfterLogout = '/customLogoutPage';

But I want to redirect the user back to where he has been, since he can see the View even without being locked in. 但是我希望将用户重定向到他曾经去过的地方,因为即使没有被锁定,他也可以看到View。

I tried something in this direction: 我试着朝这个方向努力:

protected $redirectAfterLogout = redirect()->back();

But my browser says: "Unexpected '(', expecting ',' or ';' 但我的浏览器说:“意外'(',期待','或';'

How is it possible to use a redirect back to the view where the user has been, before he logged out. 在退出之前,如何使用重定向回到用户所在的视图。

The built-in logout-method only accepts a string, you are passing a function to it. 内置的logout-method只接受一个字符串,你正在向它传递一个函数。 If you want this behaviour you have to implement your own logout-method in your AuthController . 如果您想要这种行为,您必须在AuthController实现自己的logout方法。

Fortunately, this is very simple: 幸运的是,这很简单:

public function getLogout()
{
    Auth::logout();

    return redirect()->back();
}

That's it. 而已。

For reference, this is the original function used by Laravels AuthenticatesUser trait: 作为参考,这是Laravels AuthenticatesUser特性使用的原始函数:

/**
 * Log the user out of the application.
 *
 * @return \Illuminate\Http\Response
 */
public function getLogout()
{
    Auth::logout();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}
public function getLogout()
{
    Auth::logout();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/customLogoutPage');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM