php 如何从 Laravel 5 中的请求中检索 url 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38741084/
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
How to retrieve a url parameter from request in Laravel 5?
提问by Alexander Lomia
I want to perform certain operations with a model in a middleware. Here is an example of what I want to achieve:
我想用中间件中的模型执行某些操作。这是我想要实现的一个例子:
public function handle($request, Closure $next)
{
$itemId = $request->param('item'); // <-- invalid code, serves for illustration purposes only
$item = Item::find($itemId);
if($item->isBad()) return redirect(route('dont_worry'));
return $next($request);
}
My question is, how can I retrieve the desired parameter from the $request
?
我的问题是,如何从$request
?
回答by Justin Origin Broadband
If the parameter is part of a URL and this code is being used in Middleware, you can access the parameter by it's name from the route given:
如果参数是 URL 的一部分并且此代码正在中间件中使用,则您可以通过给定路由的名称访问参数:
public function handle($request, Closure $next)
{
$itemId = $request->route()->getParameter('item');
$item = Item::find($itemId);
if($item->isBad()) return redirect(route('dont_worry'));
return $next($request);
}
This is based on having a route like: '/getItem/{item}'
这是基于具有如下路线: '/getItem/{item}'
回答by Ilya Yaremchuk
public function handle(Request $request, Closure $next)
{
$itemId = $request->item;
//..............
}