Laravel中的身份验证和防护(保护路由)

时间:2020-01-24 07:51:29

标签: php laravel authentication laravel-5

我有管理员'admin'。

我在Laraver Guard中知道如何作为多身份验证(我可以同时以用户身份和管理员身份登录)。

但是当我以admin身份登录并尝试使用auth中间件检查页面时遇到关于ERR_TOO_MANY_REDIRECTS的错误时,我感到非常惊讶。像admin这样的si没有身份验证用户没有身份验证? 拥有这样的代码

Route::group([ 'namespace' => 'Shop','middleware' => ['auth'], 'prefix' => 'shop'], function () {
    Route::get('/', 'MainController@index')->name('shop');
    Route::get('/search', 'MainController@search')->name('shop.search');
    Route::get('/ajax-search', 'MainController@ajaxSearch')->name('shop.ajax-search');
    Route::get('autocomplete', 'MainController@autocomplete')->name('shop.autocomplete');
    Route::get('/searchbyname', 'MainController@searchbyname')->name('shop.searchbyname');


});
Route::group(['prefix' => 'cart','middleware' => ['auth:admin'], 'namespace' => 'Shop'], function () {
    Route::get('/','CartController@index')->name('shop.cart');
    Route::get('/add','CartController@add')->name('cart.add');
    Route::get('/details','CartController@details')->name('cart.details');
    Route::delete('/{id}','CartController@delete')->name('cart.delete');
});

如果我想让管理员作为身份验证用户+管理员路由拥有所有访问权限,怎么解决此问题?

2 个答案:

答案 0 :(得分:0)

也许对您有帮助

Route::get('/', function () {
...})->middleware('first', 'second');

multiple middleware to the route

答案 1 :(得分:0)

您可以在Route::group中声明2个中间件。例如:

Route::group([ 'namespace' => 'Shop','middleware' => ['auth', 'auth:admin'], 'prefix' => 'shop'], function () {
    ...
});