[英]How to logout and redirect to login page using Laravel 5.4?
I am using Laravel 5.4 and trying to implement authentication system.我正在使用 Laravel 5.4 并尝试实施身份验证系统。 I used php artisan command make:auth to setup it.
我使用 php artisan 命令 make:auth 来设置它。 I edited the views according to my layout.
我根据我的布局编辑了视图。 Now, when I am trying to logout it throwing me this error
现在,当我试图注销时,它抛出了这个错误
NotFoundHttpException in RouteCollection.php line 161: RouteCollection.php 第 161 行中的 NotFoundHttpException:
could any one help me how to logout?谁能帮我如何注销?
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
,或者如果您创建了logout button
,请将 href 添加到/logout
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();
你说你已经运行了
php artisan make:auth
,它应该也插入了Auth::routes();
in your routes/web.php
routing files.在你的
routes/web.php
路由文件中。 Which comes with default logout
route already defined and is named logout
.它带有已经定义的默认
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
requires POST
as HTTP request method.然后再次请注意
logout
需要POST
作为 HTTP 请求方法。 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 .这背后有很多正当理由,但只提一个非常重要的原因是,您可以通过这种方式防止跨站点请求伪造。
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() }}
!
您可以在控制器中使用以下内容:
return redirect('login')->with(Auth::logout());
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');
}
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');
});
I recommend you stick with Laravel auth routes in web.php: Auth::routes()
我建议你在 web.php 中坚持使用 Laravel auth 路由:
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.您将需要使用 POST 表单注销。 This way you will also need the CSRF token which is recommended.
这样,您还需要推荐的 CSRF 令牌。
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit">Logout</button>
</form>
In 5.5在 5.5
adding添加
Route::get('logout', 'Auth\\LoginController@logout');
to my routes file works fine.到我的路由文件工作正常。
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.使用带有使用 POST 表单注销的 Achor 标签。 This way you will also need the CSRF token.
这样,您还需要 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>
If you used the auth scaffolding in 5.5 simply direct your href
to:如果您在 5.5 中使用了 auth 脚手架,只需将您的
href
指向:
{{ route('logout') }}
There is no need to alter any routes or controllers.无需更改任何路由或控制器。
It's better and safer to add to your LoginController.php the following code, that runs only after the standard logout:将以下代码添加到 LoginController.php 中更好更安全,该代码仅在标准注销后运行:
use AuthenticatesUsers;
protected function loggedOut(Request $request)
{
return redirect('/new/redirect/you/want');
}
if you are looking to do it via code on specific conditions, here is the solution worked for me.如果您希望通过特定条件下的代码来实现,这里是对我有用的解决方案。 I have used in middleware to block certain users: these lines from below is the actual code to logout:
我在中间件中使用过来阻止某些用户:下面的这些行是要注销的实际代码:
$auth = new LoginController();
$auth->logout($request);
Complete File:完整文件:
namespace App\Http\Middleware;
use Closure;
use Auth;
use App\Http\Controllers\Auth\LoginController;
class ExcludeCustomers{
public function handle($request, Closure $next){
$user = Auth::guard()->user();
if( $user->role == 3 ) {
$auth = new LoginController();
$auth->logout($request);
header("Location: https://google.com");
die();
}
return $next($request);
}
}
I know this question was asked for old version of laravel.我知道这个问题是针对旧版本的 laravel 提出的。 I want to share my solution for Laravel 8.65 version.
我想分享我的Laravel 8.65版本解决方案。 In AuthenticatedSessionControler update destroy function like this.
在AuthenticatedSessionControler 中像这样更新销毁函数。
public function destroy(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/admin');
}
update your last line from return redirect('/')
to return redirect('/admin');
将最后一行从
return redirect('/')
为return redirect('/admin');
go to routes directory open web.php and write logout route: go 到路由目录打开 web.php 并写入注销路由:
use App\Http\Controllers\Auth\LoginController;
Route::get('logout', [LoginController::class,'logout']);
From blade add logout URL从刀片添加注销 URL
<a class="dropdown-item" href={{route('logout')}}>Logout</a>
logout function will redirect to home page /
注销 function 将重定向到主页
/
If you want to redirect to login page you should override on logout function in App\Http\Controllers\Auth\LoginController.php
如果你想重定向到登录页面,你应该在
App\Http\Controllers\Auth\LoginController.php
中覆盖注销 function
public function logout(){
return redirect('login')->with(Auth::logout());
}
Just in case someone is interested on this topic for Laravel 8 or 9.以防万一有人对此主题感兴趣,请致电 Laravel 8 或 9。
As you can read on the Laravel documentation on https://laravel.com/docs/9.x/authentication#logging-out正如您可以在 https 上阅读 Laravel 文档一样://laravel.com/docs/9.x/authentication#logging-out
In addition to Auth::logout(), you will need to invalidate the user's session and regenerate their CSRF token:除了 Auth::logout() 之外,您还需要使用户的 session 无效并重新生成他们的 CSRF 令牌:
public function logout(Request $request)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.