在 Laravel 中通过 link 注销
Logging out via a link in Laravel
我的顶部导航栏中有一个 "Logout" link。我想知道如何才能做到在我登录时,当我单击它时它会注销我并 return 我到主页。
具体来说,我对 Laravel 中的哪些文件进行了哪些更改?另外,我需要在当前仅包含 HTML 的视图中编写什么代码来触发它?
使用logout()
方法:
auth()->logout();
或者:
Auth::logout();
To log users out of your application, you may use the logout
method on the Auth
facade. This will clear the authentication information in the user's session.
2019 年 12 月 28 日编辑:有效,但此答案包含严重的安全问题。使用前请三思。 Lucas Bustamante
的回答可能是更好的选择。请参阅此答案的评论部分。
1) 如果您正在使用 laravel 包含的身份验证脚手架。你可以这样做,在你的导航栏中添加:
<a href="{{ url('/logout') }}"> logout </a>
然后将其添加到您的 web.php 文件
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
完成。这将使您注销并重定向到主页。要获取 auth 脚手架,请从命令行 cd 进入您的项目根目录并 运行
php artisan make:auth
2) 将此添加到您的导航栏:
<a href="{{ url('/logout') }}"> logout </a>
然后将其添加到您的 web.php 文件中
Route::get('/logout', 'YourController@logout');
然后在 YourController.php 文件中,添加这个
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
完成。
阅读:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
当你 运行 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>
如果您想使用 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>
如果你使用守卫,你可以使用这行代码注销:
Auth::guard('you-guard')->logout();
在 laravel 8.x
@csrf
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Logout') }}
</x-jet-dropdown-link>
</form>
由于接受的答案提到通过 GET
注销有副作用,您应该使用已经由 Laravel auth 创建的默认 POST
路由。
只需创建一个小表格并通过 link 或按钮 HTML 标签提交:
<form action="{{ route('logout') }}" method="POST">
@csrf
<button type="submit">
{{ __('Logout') }}
</button>
</form>
我的顶部导航栏中有一个 "Logout" link。我想知道如何才能做到在我登录时,当我单击它时它会注销我并 return 我到主页。
具体来说,我对 Laravel 中的哪些文件进行了哪些更改?另外,我需要在当前仅包含 HTML 的视图中编写什么代码来触发它?
使用logout()
方法:
auth()->logout();
或者:
Auth::logout();
To log users out of your application, you may use the
logout
method on theAuth
facade. This will clear the authentication information in the user's session.
2019 年 12 月 28 日编辑:有效,但此答案包含严重的安全问题。使用前请三思。 Lucas Bustamante
的回答可能是更好的选择。请参阅此答案的评论部分。
1) 如果您正在使用 laravel 包含的身份验证脚手架。你可以这样做,在你的导航栏中添加:
<a href="{{ url('/logout') }}"> logout </a>
然后将其添加到您的 web.php 文件
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
完成。这将使您注销并重定向到主页。要获取 auth 脚手架,请从命令行 cd 进入您的项目根目录并 运行
php artisan make:auth
2) 将此添加到您的导航栏:
<a href="{{ url('/logout') }}"> logout </a>
然后将其添加到您的 web.php 文件中
Route::get('/logout', 'YourController@logout');
然后在 YourController.php 文件中,添加这个
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
完成。
阅读:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
当你 运行 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>
如果您想使用 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>
如果你使用守卫,你可以使用这行代码注销:
Auth::guard('you-guard')->logout();
在 laravel 8.x
@csrf <x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Logout') }}
</x-jet-dropdown-link>
</form>
由于接受的答案提到通过 GET
注销有副作用,您应该使用已经由 Laravel auth 创建的默认 POST
路由。
只需创建一个小表格并通过 link 或按钮 HTML 标签提交:
<form action="{{ route('logout') }}" method="POST">
@csrf
<button type="submit">
{{ __('Logout') }}
</button>
</form>