Jie*_*eng 11 php forwarding symfony
是否可以转发请求,传递所有GET/POST参数?
我想如果我这样做的话
$this->forward('dest')
Run Code Online (Sandbox Code Playgroud)
我会去dest没有任何GET/POST参数?
UPDATE
我的目标实际上是有一个控制器动作,就像addSomething检查用户有足够的"项目"来添加一些东西.然后将请求转发给approperiate控制器以继续实际添加{Type} Something
或者在所有进行检查的控制器中获得"检查"服务更合适?无论如何,我认为它有助于知道如何使用所有参数转发到控制器动作
Ino*_*ryy 28
最简单的解决方案(也许我可能会选择)只是将Request类作为forward参数传递
public function indexAction()
{
$request = $this->getRequest();
return $this->forward('AcmeBundle:Forward:new', array('request' => $request));
}
Run Code Online (Sandbox Code Playgroud)
在转发操作中,只需将其用作方法参数:
public function testAction($request)
{
var_dump($request);exit;
}
Run Code Online (Sandbox Code Playgroud)
Kri*_*ith 15
我没有看到任何理由通过内核转发请求.您可以按照您的建议,将此逻辑封装在检查器服务中,或者您可以创建一个kernel.request在路由器侦听器之后运行的侦听器,并仅在_controller满足条件时应用该属性.
例如,这个routing.yml:
some_route:
pattern: /xyz
defaults: { _controller_candidate: "FooBundle:Bar:baz" }
Run Code Online (Sandbox Code Playgroud)
而这个听众:
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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置为在核心路由器侦听器之后运行:
services:
my_listener:
class: MyListener
tags:
-
name: kernel.event_listener
event: kernel.request
priority: -10
Run Code Online (Sandbox Code Playgroud)
核心路由器侦听器的优先级0在Symfony 2.0和32Symfony 2.1中.无论哪种情况,都-10应该优先考虑.
我很好奇,看看这是否有效:)
Gen*_*ire 12
所有POST参数都会自动转发.在目标控制器中使用POST参数不需要任何操作.但是你必须明确地传递查询(GET)参数和路径参数.forward方法采用2个optionals参数分别表示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);
}
Run Code Online (Sandbox Code Playgroud)