gpt4 book ai didi

php - ZF2 : How to pass parameters to forward plugin which I can then get in the method I forward them to?

转载 作者:可可西里 更新时间:2023-11-01 12:42:53 28 4
gpt4 key购买 nike

我在 Foo Controller 中有一个需要参数的 Action 方法:

public function fooAction($one, $two) {
$a = one;
$b = $two;
}

我需要从某个 Boo Controller 的其他方法转发到该方法。其中一个参数必须是引用参数。唯一的例子 the manual有是这样的:

$result = $this->forward()->dispatch('Boo\Controller\Boo', array('action' => 'boo'));

没有任何附加参数。但是他们写道:

$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,
));

但它不起作用。

有什么方法可以将额外的参数传递给正向 Controller 吗?

更新:

这些也不起作用:

$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,
)));

更新 2:

我仍然无法获得我想要的功能(使用 forward 插件传递参数),但我找到了其他解决方案。在调用 forward 插件之前,我将变量设置为 Request 对象,在 forward 之后,我从 Request 中获取它们> 在我的 boo 操作中我的 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 中没有任何内容。

UPD 3 和最终版本:

我终于找到了他们决定隐藏我通过 forward 插件传递的参数的位置。他们将它们放在 RouteMatch 对象中。

- 试着猜猜我们把你的参数藏在哪里了......哦,是的,它们在 RouteMatch 中,它们当然在那里,你不觉得还有其他的吗?

手册的forward 插件部分没有任何信息!

要获取参数,我必须在我的 BooController::booAction 中执行此操作:

$param = $this->getEvent()->getRouteMatch()->getParam('nameOfParam');

最佳答案

为什么不使用 params 插件?

这对我有用:

public function indexAction() {

$object = new SomeObject();

return $this->forward()->dispatch('Application\Controller\Index', [
'action' => 'show',
'myObject' => $object,
]);
}

public function showAction() {

$object = $this->params('myObject');

var_dump($object);

return [];
}

关于php - ZF2 : How to pass parameters to forward plugin which I can then get in the method I forward them to?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13766039/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com