欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

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

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

To set the redirect after logout I set the property in my AuthController like this:

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.

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.

shareimprove this question
 

2 Answers 正确答案

The built-in logout-method only accepts a string, you are passing a function to it. If you want this behaviour you have to implement your own logout-method in your AuthController.

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:

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

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

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

来自  http://stackoverflow.com/questions/33207271/laravel-5-redirect-after-logout-how-to-redirect-back

普通分类: