我在Laravel 5.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
如果我清除Google的Chrome缓存,则可以正常使用。
答案 0 :(得分:37)
我在Laravel 5.2中也有类似的问题。你应该改变你的路线
Route::get('auth/logout', 'Auth\AuthController@logout');
或在AuthController构造函数中添加
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}
这对我有用。
答案 1 :(得分:6)
使用下面的代码
Auth::logout();
或
auth()->logout();
答案 2 :(得分:4)
问题来自'客人' AuthController构造函数中的中间件。它应该从$this->middleware('guest', ['except' => 'logout']);
更改为$this->middleware('guest', ['except' => 'getLogout']);
如果检查内核文件,则可以看到您的访客中间件指向\App\Http\Middleware\RedirectIfAuthenticated::class
此中间件检查用户是否经过身份验证,并在验证后将用户重定向到根页,但如果未经过身份验证,则允许用户执行操作。通过使用$this->middleware('guest', ['except' => 'getLogout']);
,调用getLogout函数时不会应用中间件,从而使经过身份验证的用户可以使用它。
N / B:与原始答案一样,您可以将getLogout
更改为logout
,因为getLogout方法只是在laravel的实现中返回logout方法。
答案 3 :(得分:2)
在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
,而不是首先调用/
的{{1}},然后再将中间件重定向到{{1} }}
答案 4 :(得分:1)
这应该是AuthController
中构造函数的内容<?php // Script 12.5 - add_entry.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Connect and select:
$dbc = mysql_connect('localhost', 'root', '*******');
mysql_select_db('myblog', $dbc);
// Validate the form data
$problem = FALSE;
if (!empty($_POST['title']) && !empty($_POST['entry'])) {
$title = trim(strip_tags(S_POST['title']));
$entry = trim(strip_tags($_POST['entry']));
} else {
print '<p style="color: red;">Please submit both a title and an entry.</p>';
$problem = TRUE;
}
if (!$problem) {
// Define the query:
$query = "INSERT INTO entries (entry_id, title, entry, date_entered) VALUES (0, '$title', '$entry', NOW())";
if (@mysql_query($query, $dbc)) {
print '<p>The blog entry has been added!<p>';
} else {
print '<p style="color: red;">Could not add the entry because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was:' . $query . '</p>';
}
} // No Problem!
mysql_close($dbc); // Close the connection
} // End of form submission IF
?>`
答案 5 :(得分:0)
在routes.php
档案Route::get('auth/logout', 'Auth\AuthController@getLogout');
中添加此行
并在您的视图中添加此项
<a href="{{ url('/auth/logout') }}" > Logout </a>
它适用于我
答案 6 :(得分:0)
只需添加以下路线,不要在任何路线组(中间件)中添加:
Route::get('your-route', 'Auth\AuthController@logout');
现在注销应该在L 5.2中正常工作,而不修改AuthController
中的任何内容。
答案 7 :(得分: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();
}