php Laravel 5.2:Auth::logout() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34479994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2: Auth::logout() is not working
提问by Felipe Pe?a
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');
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
编辑 1
If I clear Google's Chrome cache, it works.
如果我清除 Google 的 Chrome 缓存,它就可以工作。
回答by Aztecnologic
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.
那对我有用。
回答by Nimisha Molia
use below code
使用下面的代码
Auth::logout();
or
或者
auth()->logout();
回答by nonybrighto
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 构造函数中的“访客”中间件。应该从改变$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 方法。
回答by Hekmat
In Http->Middleware->Authenticate.php
change login
in else statement to /
在else 语句中Http->Middleware->Authenticate.php
更改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');
然后在中间件中重定向到/
回答by Napoleon256611
This should be the content of your constructor in AuthController
这应该是 AuthController 中构造函数的内容
$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);
回答by Nassima Noufail
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 </a>
it works fine for me
在routes.php
文件中添加这一行Route::get('auth/logout', 'Auth\AuthController@getLogout');
并将其添加到您的视图中
<a href="{{ url('/auth/logout') }}" > Logout </a>
它对我来说很好用
回答by im_tsm
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
.
回答by Mehran
/**
* 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();
}