简体   繁体   中英

Logout button not working in laravel 5.2

I'm using laravel 5.2 framework and I have created auth with the php artisan make:auth command. I have this in my AuthController:

protected $redirectTo = '/';
protected $redirectPath = '/students';
protected $loginPath = '/auth/login';

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

And these in my routes.php file:

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');
});

While logging in/registering everything works perfect but when i press the logout button nothing changes and I'm still logged in. What I want is to redirect to the auth/login view after clicking that Logout button. Please help.

The problems come AuthController middleware since the default router name is " logout " and if you have change the name middleware will not recognized your router name.

You just need to keep the default name router OR fix your construct middleware.

Route::get('logout', [ 'uses' => 'Auth\AuthController@getLogout', 'as' => 'logout' ]);

In Authcontroller ,

public function __construct(){
   $this->middleware('guest', [ 'except' => 'logout' ]); // Default router name is "logout"
}

Hope it will help you :-)

Add this to AuthController.php

    public function getLogout(){
      return redirect('Auth/login');

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM