13

我在 Laravel 5.2 中构建了一个非常简单的应用程序,但是当使用AuthController's 操作注销时,它根本不起作用。我有一个导航栏,用于检查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

如果我清除 Google 的 Chrome 缓存,它就可以工作。

4

8 回答 8

38

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

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

或在 AuthController 构造函数中添加

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

这对我有用。

于 2016-01-07T23:58:42.373 回答
6

使用下面的代码

Auth::logout();

或者

auth()->logout();
于 2015-12-31T09:54:04.590 回答
4

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

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

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

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

于 2016-04-10T16:51:42.953 回答
2

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

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

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

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

用于注销调用以下功能:

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

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

于 2016-04-09T06:48:08.160 回答
1

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

$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);
于 2016-01-14T02:28:41.320 回答
0

routes.php在文件中添加这一行并将其Route::get('auth/logout', 'Auth\AuthController@getLogout'); 添加到您的视图中 <a href="{{ url('/auth/logout') }}" > Logout </a> 它对我来说很好

于 2016-08-12T01:06:06.847 回答
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();
}
于 2017-05-26T06:32:03.230 回答
0

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

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

现在注销应该可以在 L 5.2 中正常工作,而无需修改AuthController.

于 2016-08-13T06:12:21.333 回答