[英]Symfony: how to pass a parameter from Route to Controller with no path
I have a route 我有一条路线
detail:
path: /{code}
defaults: { _controller: controller.main:detailAction }
I also have a controller for this route 我也有这条路线的控制器
public function detailAction(Request $request, string $code, int $size, array $params): Response
{
}
My question is: how can I say to controller which parameters he should take as int $size
and array $params
? 我的问题是:我该如何告诉控制器他应将哪些参数作为int $size
和array $params
? I have found in symfony docs that I may specifically mention params in defaults
section with default values like this 我在symfony文档中发现,我可能特别在defaults
部分提到了params,像这样的默认值
detail:
path: /{code}
defaults: { _controller: controller.main:detailAction }
size: 1
params: "Hello world!"
But that is not what I want since I shouldn't have a default value for this params but it ought to be taken directly from request. 但这不是我想要的,因为我不应该为此参数设置默认值,但是应该直接从请求中获取它。 How do I do this without making my route like /{code}/{size}
? 如何在不使路径像/{code}/{size}
那样的情况下执行此操作? And even in this case what do I do with an array? 即使在这种情况下,我该如何处理数组?
You can generate a url like this by passing parameters in your controller: 您可以通过在控制器中传递参数来生成这样的网址:
$url = $this->generateUrl("detail", array("code" => $code, ...));
return $this->redirect($url);
And routing: 和路由:
detail:
path: /
defaults: { _controller: controller.main:detailAction }
If you want to specify parameters like this 如果要指定这样的参数
someurl.io/action/?filter=allopenissues&orderby=created
You should typehint Request object in your action and access its query parameters bag. 您应该在操作中键入提示Request对象,并访问其查询参数包。 If your controller extends Symfony Controllers, Request will be automatically passed. 如果您的控制器扩展了Symfony控制器,则请求将自动传递。
use Symfony\Component\HttpFoundation\Request;
....
public function updateAction(Request $request)
{
$request->query->get('myParam'); // get myParam from query string
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.