我想在用户成功登出后重定向回用户所在的位置,因为我有即使登出也可以访问的方法。
我保护我的PhotosController中的每个方法,除了@show
public function __construct()
{
$this->middleware('auth', ['except' => 'show']);
}
要在退出后设置重定向,我在AuthController中设置属性,如下所示:
protected $redirectAfterLogout = '/customLogoutPage';
但是我想把用户重定向到他曾经去过的地方,因为他可以看到视图,即使没有被锁定。
我试了一下这个方向:
protected $redirectAfterLogout = redirect()->back();
但是我的浏览器显示:"Unexpected '(', expected ',' or ';'
如何使用重定向返回到用户注销前的视图
内置的logout方法只接受字符串,您正在向它传递一个函数。如果你想要这种行为,你必须在你的AuthController
中实现你自己的注销方法。
幸运的是,这很简单:
public function getLogout()
{
Auth::logout();
return redirect()->back();
}
。
作为参考,这是Laravels AuthenticatesUser
trait使用的原始函数:
/**
* 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');
}