我正在使用laravel 5.2框架,并且我使用php artisan make:auth
命令创建了身份验证。我的身份验证控制器中有这个:
protected $redirectTo = '/';
protected $redirectPath = '/students';
protected $loginPath = '/auth/login';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
而这些在我的路线.php文件中:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
Route::get('/', 'HomeController@index');
Route::get('/students/', 'StudentsController@showStudents');
// Authentication routes...
Route::get('auth/login', 'Auth'AuthController@getLogin');
Route::post('auth/login', 'Auth'AuthController@postLogin');
Route::get('auth/logout', 'Auth'AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'Auth'AuthController@getRegister');
Route::post('auth/register', 'Auth'AuthController@postRegister');
});
登录/注册时一切正常,但是当我按下注销按钮时没有任何变化,我仍然登录。我想要的是单击该注销按钮后重定向到身份验证/登录视图。请帮忙。
问题出在AuthController中间件上,因为默认路由器名称是" logout
",如果您更改了名称,中间件将无法识别您的路由器名称。
您只需要保留默认名称路由器或修复构造中间件。
Route::get('logout', [ 'uses' => 'Auth'AuthController@getLogout', 'as' => 'logout' ]);
在Authcontroller
,
public function __construct(){
$this->middleware('guest', [ 'except' => 'logout' ]); // Default router name is "logout"
}
希望对您有所帮助:-)
将其添加到 AuthController.php
public function getLogout(){
return redirect('Auth/login');
}