php 如何使用 Laravel 5.4 注销并重定向到登录页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43585416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to logout and redirect to login page using Laravel 5.4?
提问by Y.EzzEldin
I am using Laravel 5.4 and trying to implement authentication system. I used php artisan command make:auth to setup it. I edited the views according to my layout. Now, when I am trying to logout it throwing me this error
我正在使用 Laravel 5.4 并尝试实现身份验证系统。我使用 php artisan 命令 make:auth 来设置它。我根据我的布局编辑了视图。现在,当我尝试注销时,它会向我抛出此错误
NotFoundHttpException in RouteCollection.php line 161:
NotFoundHttpException 在 RouteCollection.php 第 161 行:
could any one help me how to logout?
任何人都可以帮助我如何注销?
回答by Tauras
In your web.php
(routes):
在您的web.php
(路线)中:
add:
添加:
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
In your LoginController.php
在你的 LoginController.php
add:
添加:
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
Also, in the top of LoginController.php
, after namespace
此外,在顶部LoginController.php
,之后namespace
add:
添加:
use Auth;
Now, you are able to logout using yourdomain.com/logout
URL or if you have created logout button
, add href to /logout
现在,您可以使用yourdomain.com/logout
URL注销,或者如果您已经创建logout button
,请将 href 添加到/logout
回答by Sid
Well even if what suggest by @Tauras just works I don't think it's the correct way to deal with this.
好吧,即使@Tauras 的建议有效,我也不认为这是处理此问题的正确方法。
You said you have run php artisan make:auth
which should have also inserted Auth::routes();
in your routes/web.php
routing files. Which comes with default logout
route already defined and is named logout
.
你说你已经运行了php artisan make:auth
,它应该也插入Auth::routes();
到你的routes/web.php
路由文件中。其中带有logout
已定义的默认路由并命名为logout
.
You can see it here on GitHub, but I will also report the code here for simplicity:
你可以在 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');
}
Then again please note that logout
requiresPOST
as HTTP request method. There are many valid reason behind this, but just to mention one very important is that in this way you can prevent cross-site request forgery.
然后再次请注意logout
需要POST
作为 HTTP 请求方法。这背后有很多正当理由,但只提一个非常重要的原因是,您可以通过这种方式防止跨站点请求伪造。
So according to what I have just pointed out a correct way to implement this could be just this:
因此,根据我刚刚指出的实现这一点的正确方法可能是这样的:
<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>
Finally note that I have inserted laravel out of the box ready function {{ csrf_field() }}
!
最后请注意,我已经插入了 laravel 开箱即用的功能{{ csrf_field() }}
!
回答by usama muneer
You can use the following in your controller:
您可以在控制器中使用以下内容:
return redirect('login')->with(Auth::logout());
回答by Somwang Souksavatd
here is another way to do it by calling Auth::logout() in route
这是通过在路由中调用 Auth::logout() 来实现的另一种方法
Route::get('/logout', function(){
Auth::logout();
return Redirect::to('login');
});
回答by Robby Alvian Jaya Mulia
Best way for Laravel 5.8
Laravel 5.8 的最佳方式
100% worked
100% 工作
Add this function inside your Auth\LoginController.php
在你的Auth\LoginController.php 中添加这个函数
use Illuminate\Http\Request;
And also add this
并添加这个
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return $this->loggedOut($request) ?: redirect('/login');
}
回答by Jonathan Roy
I recommend you stick with Laravel auth routes in web.php: Auth::routes()
我建议你坚持在 web.php 中使用 Laravel 身份验证路由: Auth::routes()
It will create the following route:
它将创建以下路由:
POST | logout | App\Http\Controllers\Auth\LoginController@logout
You will need to logout using a POST form. This way you will also need the CSRF token which is recommended.
您将需要使用 POST 表单注销。这样,您还需要推荐的 CSRF 令牌。
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit">Logout</button>
</form>
回答by Beefjeff
In 5.5
在 5.5
adding
添加
Route::get('logout', 'Auth\LoginController@logout');
Route::get('logout', 'Auth\LoginController@logout');
to my routes file works fine.
到我的路由文件工作正常。
回答by Beefjeff
If you used the auth scaffolding in 5.5 simply direct your href
to:
如果您在 5.5 中使用了身份验证脚手架,只需将您定向href
到:
{{ route('logout') }}
There is no need to alter any routes or controllers.
无需更改任何路由或控制器。
回答by Ghanshyam Nakiya
In Laravel 6.2
在 Laravel 6.2 中
Add the following route to : web.php
将以下路由添加到: web.php
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
Using Achor tag with logout using a POST form. This way you will also need the CSRF token.
使用带有使用 POST 表单注销的 Achor 标签。这样,您还需要 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>