php Symfony 2 转发请求传递 GET/POST 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8707586/
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
Symfony 2 Forward Request passing along GET/POST params
提问by Jiew Meng
Is it possible to forward a request, passing along all GET/POST params?
是否可以转发请求,传递所有 GET/POST 参数?
I think if I just do
我想如果我只是这样做
$this->forward('dest')
I will go to dest
without any GET/POST params?
我会去dest
没有任何 GET/POST 参数?
UPDATE
更新
My objective is actually to have a controller action like addSomething
that takes checks that the user has the sufficient "items" to add something. Then forward the request to the approperiate controller to continue the actual adding of adding{Type}Something
我的目标实际上是让控制器动作像addSomething
这样检查用户是否有足够的“项目”来添加一些东西。然后将请求转发给合适的控制器,继续实际添加adding{Type}Something
Or would getting a "checking" service in all controllers that does the checking be more appropriate? Anyways, I think its informative to know how to forward to a controller action with all params
或者是否会在所有控制器中获得“检查”服务以进行检查更合适?无论如何,我认为知道如何使用所有参数转发到控制器操作是很有用的
回答by Inoryy
Easiest solution (and one I'd probably go for) would be to just pass Request class as forward parameter
最简单的解决方案(我可能会采用的解决方案)是将 Request 类作为转发参数传递
public function indexAction()
{
$request = $this->getRequest();
return $this->forward('AcmeBundle:Forward:new', array('request' => $request));
}
And in forwarded action just use it as method param:
在转发操作中,只需将其用作方法参数:
public function testAction($request)
{
var_dump($request);exit;
}
回答by Kris Wallsmith
I don't see any reason here to forward the request back through the kernel. You can go the route of encapsulating this logic in a checker service, as you've suggested, or you may be able to create a kernel.request
listener that runs after the router listener and applies the _controller
attribute only if your conditions are met.
我在这里看不到任何理由将请求转发回内核。您可以按照您的建议走在检查器服务中封装此逻辑的路线,或者您可以创建一个kernel.request
在路由器侦听器之后运行的侦听器,并仅在_controller
满足您的条件时才应用该属性。
For example, this routing.yml
:
例如,这个routing.yml
:
some_route:
pattern: /xyz
defaults: { _controller_candidate: "FooBundle:Bar:baz" }
And this listener:
而这位听众:
class MyListener
{
public function onKernelRequest($event)
{
$request = $event->getRequest();
if (!$controller = $request->attributes->get('_controller_candidiate')) {
return;
}
if (/* your logic... */) {
$request->attributes->set('_controller', $controller');
}
}
}
Configured to run afterthe core router listener:
配置为在核心路由器侦听器之后运行:
services:
my_listener:
class: MyListener
tags:
-
name: kernel.event_listener
event: kernel.request
priority: -10
The priority of the core router listener is 0
in Symfony 2.0 and 32
in Symfony 2.1. In either case, a priority of -10
should work.
核心路由器侦听器的优先级0
在 Symfony 2.0 和32
Symfony 2.1 中。在任何一种情况下,优先级都-10
应该起作用。
I'm curious to see if this works :)
我很好奇这是否有效:)
回答by Genoud Magltheitroade
All POST param are automatically forwarded. No action is needed to have POST param in the target controller. But you have to explicitly pass query (GET) params and path params. The forward method take 2 optionals parameter representing respectively the pathParam and the queryParam arrays. You can just pass all the query param from the current request
所有 POST 参数都会自动转发。在目标控制器中具有 POST 参数不需要任何操作。但是您必须显式传递查询 (GET) 参数和路径参数。forward 方法采用 2 个可选参数分别表示 pathParam 和 queryParam 数组。您可以只传递当前请求中的所有查询参数
public testAction(Request $request){
$pathParam = array(); //Specified path param if you have some
$queryParam = $request->query->all();
$response = $this->forward("AcmeBundle:Forward:new", $pathParam, $queryParam);
}
回答by Dado
Forwarding "request attributes" as "path" works for me:
将“请求属性”作为“路径”转发对我有用:
public function indexAction()
{
$path = $this->getRequest()->attributes->all();
return $this->forward('CompMyBundle:MyController:MyAction', $path);
}
$path['_controller'] will be overwritten in forward() method!
$path['_controller'] 将在 forward() 方法中被覆盖!