Php Laravel 5.2:Auth::logout()不工作

Php Laravel 5.2:Auth::logout()不工作,php,laravel,laravel-5,laravel-5.2,laravel-authorization,Php,Laravel,Laravel 5,Laravel 5.2,Laravel Authorization,我正在Laravel5.2中构建一个非常简单的应用程序,但是当使用AuthController的操作注销时,它根本不起作用。我有一个导航栏,用于检查Auth::check(),在调用注销操作后它不会改变 我在routes.php文件中有此路由: Route::get('users/logout','Auth\AuthController@getLogout'); 就在外面 Route::group(['middleware'=>['web']],function()语句 我还尝试在AuthCon

我正在Laravel5.2中构建一个非常简单的应用程序,但是当使用
AuthController
的操作注销时,它根本不起作用。我有一个导航栏,用于检查
Auth::check()
,在调用注销操作后它不会改变

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

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

就在外面

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

我还尝试在AuthController.php文件的末尾添加以下操作

public function getLogout() 
{
    $this->auth->logout();
    Session::flush();
    return redirect('/');
}
你有什么想法吗

编辑1

如果我清除谷歌的Chrome缓存,它就会工作。

使用下面的代码

Auth::logout();


我在Laravel 5.2中也遇到了类似的问题。您应该将路线更改为

Route::get('auth/logout', 'Auth\AuthController@logout');
或在AuthController构造函数中添加

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

这对我很有用。

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

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

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

return redirect()->guest('/');
并在routes.php中定义以下路由

Route::get('/', function () {
    return view('login');
});
要注销,请调用以下函数:

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

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

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

return redirect()->guest('/');
如果检查内核文件,可以看到来宾中间件指向
\App\Http\middleware\RedirectIfAuthenticated::class

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


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

routes.php
文件
Route::get('auth/logout','auth\AuthController@getLogout);
并将其添加到您的视图中

它对我来说很好

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


现在,注销应该像在L5.2中一样工作,而不需要修改AuthController中的任何内容?您能详细解释一下您运行的操作以及哪些操作不起作用吗?您好@MarcinNabiałek。我编辑了这个问题。我希望能更好地解释一下。您的注销路径不应该在
web
中间件组之外。它应该在它里面。@ThomasKim仍然不起作用什么是
Auth::logout()
do?它会带来相同的结果吗?虽然它应该会带来相同的结果。无论如何,你能发布整个
AuthController
代码吗?谢谢。我会稍后检查,如果它有效,我会接受它作为答案。@Aztecnologic这解决了我的问题。不知道为什么我们必须在Laravel 5.2中编写自定义注销方法-尽管我没有必须在5.1中更新。哦,好的。谢谢。也解决了我的问题。有什么解释吗?似乎与使用旧的laravel/laravel样板库时从5.1更新到5.2有关。除了上面的答案外,您可以在注销后通过在AuthController保护的$redirectAfterLogout='auth/login'中添加以下代码来自定义重定向;Adding
getLogout
中,除了
数组对我起了作用。
/**
 * 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();
}
/**
 * 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();
}