Laravel 5身份验证中间件始终重定向到root或登录 [英] Laravel 5 authentication middleware always redirects to root or login

查看:167
本文介绍了Laravel 5身份验证中间件始终重定向到root或登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Laravel 5中保护路由时,当我未登录时它会很好地工作,因为它会将受保护的路由重定向到登录页面,但是一旦我登录并尝试访问受保护的路由,它会将我重定向到根路由. 例如,如果我尝试访问/people或/people/1,它将把我重定向到/

When I protect routes in Laravel 5 it works well when I'm not logged in because it redirects the protected routes to the login page but once I login and try to access the protected routes it redirects me to the root route. For example if I try to access /people or /people/1 it will redirect me to /

这是我的route.php文件:

Here's my routes.php file:

Route::get('/', function () {
 return view('welcome');
});

Route::group(['middleware' => ['auth']], function () {
 Route::resource('people', 'PeopleController');
 Route::resource('people.checkins', 'CheckinsController');
 Route::model('checkins', 'Checkin');
 Route::model('people', 'Person');

 Route::bind('checkins', function($value, $route) {
    return App\Checkin::whereId($value)->first();
 });
 Route::bind('people', function($value, $route) {
    return App\Person::whereId($value)->first();
 });
});

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

 Route::get('/home', 'HomeController@index');
});

推荐答案

如果您要使用Auth,则还应将"web"组也应用于这些路由.

If you are going to be using Auth you should have the 'web' group applied to those routes as well.

您可以将使用身份验证"中间件的路由组调整为:

You can adjust your route group that is using the 'auth' middleware to:

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

更新对于Laravel 5.2.27.如果您已经安装了laravel/laravel> = 5.2.27的新副本,那么您的路由将被包装在一个默认情况下应用"web"中间件的组中.这仅适用于全新安装,因为此更改是对App \ Providers \ RouteServiceProvider的更改,不会升级到laravel/framework.

UPDATE For Laravel 5.2.27. If you have installed a fresh copy of laravel/laravel >= 5.2.27 Your routes will be wrapped in a group that applies the 'web' middleware by default now. This is only for fresh installs as this change is to App\Providers\RouteServiceProvider which an upgrade to laravel/framework will not touch.

这篇关于Laravel 5身份验证中间件始终重定向到root或登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆