Laravel 5 authentication middleware always redirec

2019-05-20 00:31发布

问题:

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 /

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');
});

回答1:

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 () {
    // ...
});

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.