简体   繁体   English

在 Laravel 5 会话过期后将用户重定向到登录页面

[英]Redirect the user to the log-in page after session expired in Laravel 5

Goal目标

I'm trying to redirect my user to my login page as soon as their session expired or log-out.我试图在他们的会话过期或注销后立即将我的用户重定向到我的登录页面。

Example: if they log-out, and try to click on any link, they should get redirected to the log-in page.示例:如果他们退出并尝试点击任何链接,他们应该被重定向到登录页面。


I have my log-in route declared like this我的登录路径是这样声明的

Route::get('/',['as' => 'login', 'uses'=>'AuthController@getSignIn']);

Then, I tried to configure it in config/session.php然后,我尝试在config/session.php配置它

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/

'lifetime' => 120,
'expire_on_close' => true,
'expired-session-redirect' => url('/')

As soon as my user session is expired, I click on other linked, I didn't get redirect to my log-in page ?一旦我的用户会话过期,我点击其他链接,我没有重定向到我的登录页面?

It crashed and return the white empty page:它崩溃并返回白色空白页面:

在此处输入图片说明

What else do I need to do to achieve something like that ?我还需要做些什么来实现这样的目标?

For redirecting users who aren't signed in, you can add the auth middleware to any routes that you don't want to allow signed out users to visit.对于重定向未登录的用户,您可以将 auth 中间件添加到您不想允许已注销用户访问的任何路由。 More info on Laravel middleware documentation .有关Laravel 中间件文档的更多信息。

For a single route:对于单一路线:

Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

For a route group:对于路由组:

Route::group(['middleware' => ['auth']], function () {
    //
});

It depends on which version of Laravel you're using to determine how to set where the user is redirected to.这取决于您使用的 Laravel 版本来确定如何设置用户重定向到的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM