gpt4 book ai didi

php - 拉维尔 5 : How to get list of Parameters in Middleware

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:10 31 4
gpt4 key购买 nike

如何在中间件中检索 route 参数列表,尝试了多种方法但总是以错误或空结果结尾:

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.
}

有没有办法获取中间件中的参数列表?谢谢,

更新:(添加路线)

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

Laravel 版本:5.1.20

最佳答案

您将获得 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.

关于php - 拉维尔 5 : How to get list of Parameters in Middleware,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009054/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com