Laravel 5.2:Auth :: logout()不起作用

时间:2022-10-18 17:24:06

I'm building a very simple app in Laravel 5.2, but when using AuthController's action to log out, it just simply doesn't work. I have a nav bar which checks for Auth::check() and it doesn't change after calling the log out action.

我在Laravel 5.2中构建了一个非常简单的应用程序,但是当使用AuthController的操作注销时,它只是不起作用。我有一个导航栏,用于检查Auth :: check(),并且在调用注销操作后它不会更改。

I have this route inside the routes.php file:

我在routes.php文件中有这个路由:

Route::get('users/logout', 'Auth\AuthController@getLogout');

and it's outside the

它在外面

Route::group(['middleware' => ['web']], function () statement.

Route :: group(['middleware'=> ['web']],function()语句。

I did also try to add the follow action at the end of the AuthController.php file.

我还尝试在AuthController.php文件的末尾添加跟随操作。

public function getLogout() 
{
    $this->auth->logout();
    Session::flush();
    return redirect('/');
}

Do you have any ideas?

你有什么想法?

EDIT 1

If I clear Google's Chrome cache, it works.

如果我清除Google的Chrome缓存,则可以正常使用。

8 个解决方案

#1


36  

I also had similar problem in Laravel 5.2. You should change your route to

我在Laravel 5.2中也有类似的问题。你应该改变你的路线

Route::get('auth/logout', 'Auth\AuthController@logout');

or in AuthController constructor add

或者在AuthController构造函数中添加

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

That worked for me.

这对我有用。

#2


6  

use below code

使用下面的代码

Auth::logout();

or

auth()->logout();

#3


4  

The problem is from the 'guest' middleware in the AuthController constructor. It should be changed from $this->middleware('guest', ['except' => 'logout']); to $this->middleware('guest', ['except' => 'getLogout']);

问题来自AuthController构造函数中的“guest”中间件。它应该从$ this-> middleware('guest',['except'=>'logout'])更改; $ this-> middleware('guest',['except'=>'getLogout']);

If you check the kernel file, you can see that your guest middleware point to \App\Http\Middleware\RedirectIfAuthenticated::class

如果检查内核文件,可以看到您的访客中间件指向\ App \ Http \ Middleware \ RedirectIfAuthenticated :: class

This middleware checks if the user is authenticated and redirects the user to the root page if authenticated but lets the user carry out an action if not authenticated. By using $this->middleware('guest', ['except' => 'getLogout']); , the middleware will not be applied when the getLogout function is called, thereby making it possible for authenticated users to make use of it.

此中间件检查用户是否已通过身份验证,如果已通过身份验证,则会将用户重定向到根页,但如果未经过身份验证,则允许用户执行操作。通过使用$ this-> middleware('guest',['except'=>'getLogout']); ,调用getLogout函数时不会应用中间件,从而使经过身份验证的用户可以使用它。

N/B: As in the original answer, you can change getLogout to logout since the getLogout method simply returns the logout method in laravel's implementation.

N / B:与原始答案一样,您可以将getLogout更改为logout,因为getLogout方法只是返回laravel实现中的logout方法。

#4


2  

In Http->Middleware->Authenticate.php change login in else statement to /

在Http-> Middleware-> Authenticate.php中将else语句中的login更改为/

return redirect()->guest('/');

and define following route in routes.php

并在routes.php中定义以下路由

Route::get('/', function () {
    return view('login');
});

for logout call following function:

用于退出调用以下功能:

public function getlogout(){
    \Auth::logout();
    return redirect('/home');
}

Important: redirect to /home instead of / that first calls $this->middleware('auth'); and then in middleware redirect to /

重要提示:重定向到/ home而不是/首先调用$ this-> middleware('auth');然后在中间件重定向到/

#5


1  

This should be the content of your constructor in AuthController

这应该是AuthController中构造函数的内容

$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);

#6


0  

add this line in routes.php file Route::get('auth/logout', 'Auth\AuthController@getLogout'); and add this in your view a href="{{ url('/auth/logout') }}" > Logout it works fine for me

在routes.php文件中添加此行Route :: get('auth / logout','Auth \ AuthController @ getLogout');并在你的视图中添加一个href =“{{url('/ auth / logout')}}”>退出它对我来说很好

#7


0  

Simply add below route and do not add this inside any route group(middleware):

只需添加以下路由,不要在任何路由组(中间件)中添加:

Route::get('your-route', 'Auth\AuthController@logout');

Now logout should work as it should in L 5.2 without modifying anything in AuthController.

现在注销应该在L 5.2中工作,而不需要在AuthController中修改任何内容。

#8


0  

/**
 * Log the user out of the application.
 *
 * @param \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/');
}

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard();
}

#1


36  

I also had similar problem in Laravel 5.2. You should change your route to

我在Laravel 5.2中也有类似的问题。你应该改变你的路线

Route::get('auth/logout', 'Auth\AuthController@logout');

or in AuthController constructor add

或者在AuthController构造函数中添加

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

That worked for me.

这对我有用。

#2


6  

use below code

使用下面的代码

Auth::logout();

or

auth()->logout();

#3


4  

The problem is from the 'guest' middleware in the AuthController constructor. It should be changed from $this->middleware('guest', ['except' => 'logout']); to $this->middleware('guest', ['except' => 'getLogout']);

问题来自AuthController构造函数中的“guest”中间件。它应该从$ this-> middleware('guest',['except'=>'logout'])更改; $ this-> middleware('guest',['except'=>'getLogout']);

If you check the kernel file, you can see that your guest middleware point to \App\Http\Middleware\RedirectIfAuthenticated::class

如果检查内核文件,可以看到您的访客中间件指向\ App \ Http \ Middleware \ RedirectIfAuthenticated :: class

This middleware checks if the user is authenticated and redirects the user to the root page if authenticated but lets the user carry out an action if not authenticated. By using $this->middleware('guest', ['except' => 'getLogout']); , the middleware will not be applied when the getLogout function is called, thereby making it possible for authenticated users to make use of it.

此中间件检查用户是否已通过身份验证,如果已通过身份验证,则会将用户重定向到根页,但如果未经过身份验证,则允许用户执行操作。通过使用$ this-> middleware('guest',['except'=>'getLogout']); ,调用getLogout函数时不会应用中间件,从而使经过身份验证的用户可以使用它。

N/B: As in the original answer, you can change getLogout to logout since the getLogout method simply returns the logout method in laravel's implementation.

N / B:与原始答案一样,您可以将getLogout更改为logout,因为getLogout方法只是返回laravel实现中的logout方法。

#4


2  

In Http->Middleware->Authenticate.php change login in else statement to /

在Http-> Middleware-> Authenticate.php中将else语句中的login更改为/

return redirect()->guest('/');

and define following route in routes.php

并在routes.php中定义以下路由

Route::get('/', function () {
    return view('login');
});

for logout call following function:

用于退出调用以下功能:

public function getlogout(){
    \Auth::logout();
    return redirect('/home');
}

Important: redirect to /home instead of / that first calls $this->middleware('auth'); and then in middleware redirect to /

重要提示:重定向到/ home而不是/首先调用$ this-> middleware('auth');然后在中间件重定向到/

#5


1  

This should be the content of your constructor in AuthController

这应该是AuthController中构造函数的内容

$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);

#6


0  

add this line in routes.php file Route::get('auth/logout', 'Auth\AuthController@getLogout'); and add this in your view a href="{{ url('/auth/logout') }}" > Logout it works fine for me

在routes.php文件中添加此行Route :: get('auth / logout','Auth \ AuthController @ getLogout');并在你的视图中添加一个href =“{{url('/ auth / logout')}}”>退出它对我来说很好

#7


0  

Simply add below route and do not add this inside any route group(middleware):

只需添加以下路由,不要在任何路由组(中间件)中添加:

Route::get('your-route', 'Auth\AuthController@logout');

Now logout should work as it should in L 5.2 without modifying anything in AuthController.

现在注销应该在L 5.2中工作,而不需要在AuthController中修改任何内容。

#8


0  

/**
 * Log the user out of the application.
 *
 * @param \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/');
}

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard();
}