如何使用Laravel 5.4注销并重定向到登录页面?

问题描述 投票:26回答:7

我正在使用Laravel 5.4并尝试实现身份验证系统。我使用php artisan命令make:auth来设置它。我根据我的布局编辑了视图。现在,当我尝试注销时,它会把这个错误抛给我

RouteCollection.php第161行中的NotFoundHttpException:

任何人都可以帮我注销吗?

php laravel-5.4
7个回答
104
投票

在你的web.php(路线):

加:

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');

在你的LoginController.php

加:

public function logout(Request $request) {
  Auth::logout();
  return redirect('/login');
}

此外,在LoginController.php之后,在namespace之后

加:

use Auth;
use Illuminate\Http\Request;

现在,您可以使用yourdomain.com/logout URL注销,或者如果您已创建logout button,请将qref添加到/logout


41
投票

即使@Tauras的建议正常,我也不认为这是解决这个问题的正确方法。

你说你已经运行了php artisan make:auth,它应该在你的Auth::routes();路由文件中插入routes/web.php。其中包含已定义的默认logout路线,名为logout

你可以see it here on GitHub,但为了简单起见,我还会在这里报告代码:

    /**
     * Register the typical authentication routes for an application.
     *
     * @return void
     */
    public function auth()
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');
        // Registration Routes...
        $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        $this->post('register', 'Auth\RegisterController@register');
        // Password Reset Routes...
        $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
        $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
        $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
        $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    }

再请注意,logout需要POST作为HTTP请求方法。这背后有许多有效的原因,但仅提一个非常重要的是,通过这种方式可以防止跨站点请求伪造。

所以根据我刚刚指出的,实现这个的正确方法可能就是这样:

<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();">
    Logout
</a>    
<form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
    {{ csrf_field() }}
</form>

最后请注意,我已经插入laravel开箱即用功能{{ csrf_field() }}


8
投票

您可以在控制器中使用以下内容:

return redirect('login')->with(Auth::logout());

3
投票

这是通过在路由中调用Auth :: logout()来实现它的另一种方法

Route::get('/logout', function(){
   Auth::logout();
   return Redirect::to('login');
});

1
投票

在5.5

加入

Route::get('logout', 'Auth\LoginController@logout');

到我的路线文件工作正常。


1
投票

我建议你坚持使用web.php中的Laravel auth路线:Auth::routes()

它将创建以下路线:

POST | logout | App\Http\Controllers\Auth\LoginController@logout

您需要使用POST表单注销。这样您还需要建议的CSRF令牌。

<form method="POST" action="{{ route('logout') }}">
  @csrf
  <button type="submit">Logout</button>
</form>

0
投票

Laravel 5.8的最佳方式

100%工作

在Auth \ LoginController.php中添加此函数

use Illuminate\Http\Request;

并且还添加了这个

public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->invalidate();

    return $this->loggedOut($request) ?: redirect('/login');
}

-2
投票

如果您在5.5中使用auth脚手架,只需将您的href指向:

{{ route('logout') }}

无需改变任何路线或控制器。

© www.soinside.com 2019 - 2024. All rights reserved.