laravel 获取 Lumen 中的路由参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48369687/
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
Getting route parameters in Lumen
提问by Zoon
When trying to access Route parameters, using $request->route('id')
, in latest version of Lumen, I get an error.
$request->route('id')
在最新版本的 Lumen 中尝试访问 Route 参数时,出现错误。
lumen.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError:
Call to a member function parameter() on array
It works fine in Laravel.
它在 Laravel 中运行良好。
回答by Zoon
Lumen is so stripped down, that the Route resolves to a simple array, rather than a Route object.
Lumen 如此精简,以至于 Route 解析为一个简单的数组,而不是一个 Route 对象。
This is a problem, since the Request::route($key)
method assumes the Route will have a parameter
method.
这是一个问题,因为该Request::route($key)
方法假定 Route 将有一个parameter
方法。
But if you call Request::route(null)
, the complete Route array will be returned, looking something like this:
但是,如果您调用Request::route(null)
,将返回完整的 Route 数组,如下所示:
array(3) {
[0]=>
int(1)
[1]=>
array(2) {
["uses"]=>
string(40) "App\Http\Controllers\SomeController@index"
["middleware"]=>
array(2) {
[0]=>
string(4) "auth"
[1]=>
string(4) "example"
}
}
[2]=>
array(1) {
["id"]=>
string(36) "32bd15fe-fec8-11e7-ac6b-e0accb7a6476"
}
}
Where [2]
always seems to contain the Route parameters.
其中[2]
似乎总是包含 Route 参数。
I made a simple helper class to work with the Route parameters on Lumen. You can get, set and forget route parameters. This is works great if you need to manipulate them in your middleware.
我制作了一个简单的辅助类来处理 Lumen 上的 Route 参数。您可以获取、设置和忘记路由参数。如果您需要在中间件中操作它们,这非常有用。
Save in app/Support/RouteParam.php
: https://gist.github.com/westphalen/c3cd187007e0448bcb7fca1de091e4df
保存在app/Support/RouteParam.php
:https: //gist.github.com/westphalen/c3cd187007e0448bcb7fca1de091e4df
And simply use it like this: $id = RouteParam::get($request, 'id');
只需像这样使用它: $id = RouteParam::get($request, 'id');
Blame illuminate/http/Request.php
:
责备illuminate/http/Request.php
:
/**
* Get the route handling the request.
*
* @param string|null $param
*
* @return \Illuminate\Routing\Route|object|string
*/
public function route($param = null)
{
$route = call_user_func($this->getRouteResolver());
if (is_null($route) || is_null($param)) {
return $route;
}
return $route->parameter($param); // can't call parameter on array.
}
回答by AMS777
To get request parameters in Lumen, no matter what the HTTP verb is:
在 Lumen 中获取请求参数,无论 HTTP 动词是什么:
$name = $request->input('name');
$name = $request->input('name');
https://lumen.laravel.com/docs/master/requests#retrieving-input
https://lumen.laravel.com/docs/master/requests#retrieving-input
回答by LordZardeck
I'm not sure where you are trying to access this, but if it's from the controller, the Lumen documentation explains that it's accessed by type-hinting it in the method: https://lumen.laravel.com/docs/master/requests#accessing-the-request
我不确定你想在哪里访问它,但如果它来自控制器,Lumen 文档解释说它是通过在方法中键入提示来访问的:https: //lumen.laravel.com/docs/master/请求#访问请求
From the documentation:
从文档:
You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method like the following:
你仍然可以通过定义你的控制器方法来输入 Illuminate\Http\Request 并访问你的路由参数 id,如下所示:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Update the specified user.
*
* @param Request $request
* @param string $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}