Laravel 5 authentication middleware always redirects to root or login

只谈情不闲聊 提交于 2020-01-23 13:29:08

问题


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.



来源:https://stackoverflow.com/questions/34553345/laravel-5-authentication-middleware-always-redirects-to-root-or-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!