laravel Lumen:如何从中间件获取 url 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31273673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen: how can I get url parameters from middleware
提问by Andrea
This is my routes.php
:
这是我的routes.php
:
$app->get('/users/{id}/', ['middleware' => 'example', function () {
return "users";
}]);
This is the handle
function in the middleware:
这是handle
中间件中的功能:
public function handle($request, Closure $next)
{
// I would like to get the value of the url parameter {id} here
return $next($request);
}
Is there a way I can get the parameter id
from my middleware?
有没有办法id
从我的中间件中获取参数?
* Edit *
* 编辑 *
I'm using Lumen 5.1.0.
我正在使用Lumen 5.1.0。
回答by Phi Nguyen
There are some conventional ways in Laravel doesn't work on Lumen. And get parameter form URI in middleware is one of them. In Laravel, I just need to call $request->id
, it will work like magic. But here in order to get parameter in Lumen, I need to do something like this:
Laravel 中有一些常规方法不适用于 Lumen。在中间件中获取参数形式的 URI 就是其中之一。在 Laravel 中,我只需要调用$request->id
,它就会像魔术一样工作。但是在这里为了在 Lumen 中获取参数,我需要做这样的事情:
$request->route()[2]['id']
回答by Jeremy Harris
If the $request
value passed in is an instance of Illuminate\Http\Request
, which I think it might be, that class has a method called input()
, which lets you do exactly that:
如果$request
传入的值是 的一个实例(Illuminate\Http\Request
我认为可能是),则该类有一个名为 的方法input()
,它可以让您完全做到这一点:
You should try this:
你应该试试这个:
$id = $request->input('id');
回答by Allen Wayne
i think the latest lumen 5.6 official tutorial about middleware is no longer applicable, and out of date.
我觉得最新的lumen 5.6官方中间件教程已经不适用了,已经过时了。