Laravel:替换嵌套路由中的中间件参数

问题描述 投票:0回答:1

我有一个路由组,它将中间件应用于所有嵌套路由。我正在向它传递一个参数。

但是,在组内,我有一条特定的路线,我想为其传递不同的参数。我这样做:

Route::group(['prefix' => '/' . $languagePrefix, 'middleware' => ['sessionapi', 'abtest:0']], function () {

// other routes

Route::get('/i18n', [
    'as' => 'api:i18n',
])->middleware('abtest:1');

}

但是在中间件

handle
本身中,参数始终为0,即通用参数,即使我使用不同的参数访问路由。

怎么会?

public function handle($request, \Closure $next, string $customPar = null)
{
    // ...
    dd($customPar); // always 0
    // ... 
}

我尝试使用

->withoutMiddleware(['abtest'])->->middleware('abtest:1')
但没有成功

php laravel laravel-routing
1个回答
0
投票

删除handle方法的'$customPar = null'参数之前的type-hinting(string)。

更改代码...

public function handle($request, \Closure $next,$customPar = null) 
{
  // ...
  dd($customPar); // always 0
  // ... 
}

尝试一下..

© www.soinside.com 2019 - 2024. All rights reserved.