laravel为多个中间件组分配路由

时间:2017-10-13 13:40:04

标签: php laravel middleware

我可以将路由分配给多个中间件组吗?:

Route::group(['middleware' => 'auth:api'], function() {
    Route::any('/lookup', 'LookupController@lookup')->name('lookup');
});

Route::group(['middleware' => 'custom'], function() {
    Route::any('/lookup', 'LookupController@lookup')->name('lookup');
});

但是我不想将两个中间件组合在一个组中:

Route::group(['middleware' => ['auth:api', 'custom'], function() {
    Route::any('/lookup', 'LookupController@lookup')->name('lookup');
});

编辑:

Route::group(['middleware' => ['auth:api']], function () {
    Route::group(['middleware' => ['custom']], function () {
        Route::any('/lookup', 'LookupController@lookup')->name('lookup');
        });
    });

对我的案例不起作用,因为我希望第一组中的某些路由不在第二组中,反之亦然。

可以使用变量吗?

$lookupRoute = Route::any('/lookup', 'LookupController@lookup')->name('lookup');

        Route::group(['middleware' => 'auth:api'], function() {
            ??? $lookupRoute ??? 
        });

        Route::group(['middleware' => 'custom'], function() {
            ??? $lookupRoute ??? 
        });

0 个答案:

没有答案