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

Y.E*_*din 38 php laravel-5.4

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

RouteCollection.php第161行中的NotFoundHttpException:

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

Tau*_*ras 118

在你的web.php(路线):

加:

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Run Code Online (Sandbox Code Playgroud)

在你的 LoginController.php

加:

public function logout(Request $request) {
  Auth::logout();
  return redirect('/login');
}
Run Code Online (Sandbox Code Playgroud)

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

加:

use Auth;
Run Code Online (Sandbox Code Playgroud)

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

  • 为什么不使用 Laravel 预先构建的“auth routes”? (2认同)
  • 我发现这已经不够了,你还应该在注销函数中添加`$ request-> session() - > invalidate();`这可以防止我遇到的一些奇怪的场景. (2认同)

Sid*_*Sid 46

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

你说你已经运行了php artisan make:auth哪些应该也插入Auth::routes();你的routes/web.php路由文件中.其中logout已经定义了默认路由,并且已命名logout.

你可以在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');
    }
Run Code Online (Sandbox Code Playgroud)

然后再请注意,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>
Run Code Online (Sandbox Code Playgroud)

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


Usa*_*nir 14

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

return redirect('login')->with(Auth::logout());
Run Code Online (Sandbox Code Playgroud)

  • 这毫无意义。`with()` 用于将数据刷新到会话并接受字符串或数组。为什么要把 `Auth::logout()` 放在那里?它甚至不返回任何内容,因为它的返回类型是 void。 (2认同)

Rob*_*lia 13

Laravel 5.8 的最佳方式

100% 工作

在你的Auth\LoginController.php 中添加这个函数

use Illuminate\Http\Request;
Run Code Online (Sandbox Code Playgroud)

并添加这个

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

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

    return $this->loggedOut($request) ?: redirect('/login');
}
Run Code Online (Sandbox Code Playgroud)


小智 6

I recommend you stick with Laravel auth routes in web.php: Auth::routes()

It will create the following route:

POST | logout | App\Http\Controllers\Auth\LoginController@logout
Run Code Online (Sandbox Code Playgroud)

You will need to logout using a POST form. This way you will also need the CSRF token which is recommended.

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


Som*_*atd 5

这是在路由中调用Auth :: logout()的另一种方法

Route::get('/logout', function(){
   Auth::logout();
   return Redirect::to('login');
});
Run Code Online (Sandbox Code Playgroud)


Gha*_*iya 5

在 Laravel 6.2 中

将以下路由添加到: web.php

Route::post('logout', 'Auth\LoginController@logout')->name('logout');
Run Code Online (Sandbox Code Playgroud)

使用 Achor 标签并使用 POST 表单注销。这样您还需要 CSRF 令牌。

 <a class="log-out-btn" href="#" onclick="event.preventDefault();document.getElementById('logout-form').submit();"> Logout </a>

 <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
          {{ csrf_field() }}
  </form>
Run Code Online (Sandbox Code Playgroud)