通过 Laravel 中的链接注销
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43087648/
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
Logging out via a link in Laravel
提问by Ben Kao
I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.
我的顶部导航栏中有一个“注销”链接。我想知道如何才能做到这一点,当我登录时,它会在我单击它时将我注销并返回主页。
To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?
具体来说,我在 Laravel 中对哪些文件进行了哪些更改?另外,我需要在当前只包含 HTML 的视图中编写什么代码来触发它?
回答by Lucas Bustamante
When you run php artisan make:auth
, the default app.php in Laravel 5.5 does it like this:
当你运行时php artisan make:auth
,Laravel 5.5 中的默认 app.php 是这样的:
<a href="{{ route('logout') }}" 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>
回答by Dammy joel
Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante
maybe a better choice. Refer to the comment section of this answer.
2019 年 12 月 28 日编辑:可行,但此答案包含严重的安全问题。使用前请三思。答案Lucas Bustamante
也许是更好的选择。请参阅此答案的评论部分。
1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:
1) 如果您使用的是 laravel 包含的 auth scaffold。你可以这样做,在你的导航栏中添加:
<a href="{{ url('/logout') }}"> logout </a>
then add this to your web.php file
然后将其添加到您的 web.php 文件中
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run
完毕。这将使您注销并重定向到主页。要获取身份验证脚手架,请从命令行 cd 进入您的项目根目录并运行
php artisan make:auth
2) add this to your navigation bar:
2)将此添加到您的导航栏:
<a href="{{ url('/logout') }}"> logout </a>
then add this in your web.php file
然后将其添加到您的 web.php 文件中
Route::get('/logout', 'YourController@logout');
then in the YourController.php file, add this
然后在 YourController.php 文件中,添加这个
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
Done.
完毕。
Read:
读:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
回答by Alexey Mezenin
回答by eylay
if you want to use jQuery instead of JavaScript:
如果你想使用 jQuery 而不是 JavaScript:
<a href="javascript:void" onclick="$('#logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
@csrf
</form>
回答by Abd Abughazaleh
If you use guard you can logout using this line of code :
如果您使用守卫,您可以使用以下代码行注销:
Auth::guard('you-guard')->logout();