Laravel 5:如何获取中间件中的参数列表

时间:2017-02-02 17:51:21

标签: php laravel laravel-5

如何在Middleware中检索public function handle($request, Closure $next) { $request->route()->parameter('page'); //Call to a member function parameter() on null $request->route()->parameters(); //Call to a member function parameters() on null Request::capture()->getParameter('page'); //Call to undefined method Illuminate\Http\Request::getParameter() Route::getCurrentRoute()->getParameter('page'); //Call to a member function getParameter() on null Route::getCurrentRoute()->getParameters(); //Call to a member function getParameters() on null Route::getParameter('page'); //Method getParameter does not exist. } 参数列表,尝试多种方式但始终以Error或空结果结束:

Route::get('test/{page}', array('uses'=>'test@test'));

有没有办法获取Middleware中的参数列表? 感谢,

更新:(添加路线)

{{1}}

Laravel版本:5.1.20

3 个答案:

答案 0 :(得分:1)

您获得NULL值,因为您在解析路由之前将中间件定义为全局和全局中间件运行。

如果您需要访问路由参数,请将中间件用作App\Http\Kernel::$routeMiddleware中的路由中间件:

protected $routeMiddleware = [
    'my.middleware' => [
        \App\Http\Middleware\MyMiddleware::class,
     ],
];

Proposals to change this behavior have been rejected by the Laravel maintainers due to architectural concerns.

答案 1 :(得分:0)

使用request()全球帮助:

request()->route('page');

答案 2 :(得分:0)

$request->route()->parameters();