问题描述
限时送ChatGPT账号..我在 Foo 控制器中有一个需要参数的 Action 方法:
I have an Action method in Foo Controller which requires parameters:
public function fooAction($one, $two) {
$a = one;
$b = $two;
}
而且我需要从某个 Boo 控制器的另一个方法转发到该方法.这些参数之一必须是通过引用参数.手册 是这样的:
And I need to forward to that method from the other method of some Boo Controller. And one of those parameters has to be by reference parameter. The only example that the manual has is this:
$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));
没有任何附加参数.但他们写道:
No any additional parameters. But they write:
$params 是一个可选的参数数组,用于查看用于此特定请求的 RouteMatch 对象.
$params is an optional array of parameters with which to see a RouteMatch object for purposes of this specific request.
所以,我尝试了:
$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
'action' => 'boo',
'one' => &$one,
'two' => $two,
));
但它不起作用.
有没有办法将额外的参数传递给转发控制器?
Is there any way to pass additional parameters to forward controller?
UPD:
这些也不起作用:
$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
'action' => 'boo',
'params' => array(
'one' => &$one,
'two' => $two,
)));
$result = $this->forward()->dispatch('Boo\Controller\Boo', array(
'action' => 'boo',
'options' => array(
'one' => &$one,
'two' => $two,
)));
UPD 2:
我仍然无法获得我想要的功能(使用 forward
插件传递参数),但我找到了其他解决方案.在调用 forward
插件之前,我将变量设置为 Request
对象,在 forward
之后,我从 Request
获取它们> 在我的 boo 我的 Boo\Controller\BooController 的操作中:
I still can't get the functionality I want (to pass parameters with the forward
plugin) but I found other solutions. Before calling the forward
plugin I set the variables to the Request
object and after the forward
I get them from the Request
in my boo Action of my Boo\Controller\BooController:
// in Foo::fooAction
$this->getRequest()->one = &$one;
$this->getRequest()->two = $two;
$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));
// in Boo::booAction
$a = $this->getRequest()->one;
$b = $this->getRequest()->two;
愚蠢的解决方案,它不适用于 Ajax 请求.仍然对如何使用 forward 插件传递参数感兴趣.或者可能是如何在 booAction 中获取它们.因为如果我使用 forward
传递它们,Request
中没有任何内容.
Stupid solution, it will not work with Ajax requests. Still interested how to pass parameters with the forward plugin. OR MAYBE how to get them in the booAction. Because there in no anything in the Request
if I pass them with the forward
.
UPD 3 和决赛:
我终于找到了他们决定隐藏我通过 forward
插件传递的参数的地方.他们将它们放在 RouteMatch
对象中.
I finally found where they've decided to hide parameters I pass with the forward
plugin. They put them in the RouteMatch
object.
- 试着猜猜我们把你的参数藏在哪里了......哦,是的,它们在 RouteMatch
中,当然它们在那里,你不觉得别的吗?
- Tryyyy to guess where we've hidden your params... Oh yeeah, they are in the RouteMatch
, of course they are there, didn't you think smth else?
手册的 forward
插件部分中没有任何信息!
And NO ANY info in the forward
plugin section of the manual!
要获取参数,我必须在我的 BooController::booAction
中执行此操作:
To get params, I have to do this in my BooController::booAction
:
$param = $this->getEvent()->getRouteMatch()->getParam('nameOfParam');
推荐答案
您可以创建一个容器类并在两个控制器中使用它
You can create a container class and use it in both controllers
在 module.conf 中
in module.conf
public function getServiceConfig()
{
return array(
'invokables' => array(
'my_handy_container' => 'path\container_class_name',
)
);
}
在两个控制器中创建一个 getter:
Create a getter in both controllers:
public function getMyHandyContainer()
{
if (!$this->myHandyContainer) {
$this->myHandyContainer = $this->getServiceLocator()->get('my_handy_container');
}
return $this->myHandyContainer;
}
并使用:
$myContainer = $this->getMyHandyContainer()->myHandyContainer;
$myContainer->foo = 5; // set something
ZF2 使用 forward 传递变量的方法
在传递方法中做:
ZF2 way to pass vars using forward
In the passing method do:
return $this->forward()->dispatch('controller_name', [
'action' => 'whatever',
'varname' => $value,
'varname2' => $value2
]);
在调用的控制器方法中,执行:
In the invoked controller method, do:
$param2 = $this->params()->fromRoute('varname2',false);
这篇关于ZF2:如何将参数传递给转发插件,然后我可以在将它们转发到的方法中获取这些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论