8

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

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

我需要从一些Boo控制器的其他方法转发到该方法。其中一个参数必须是引用参数。该手册的唯一示例是:

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

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

$params 是一个可选参数数组,用于查看 RouteMatch 对象以用于此特定请求。

所以,我尝试了:

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

但它不起作用。

有没有办法将额外的参数传递给转发控制器?

升级版:

这些也不起作用:

$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对象,然后从我的Boo\Controller\BooController的boo Action 中forward获取它们:Request

// 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。因为Request如果我将它们与forward.

UPD 3 和最终版本:

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

- 试着猜猜我们把你的参数藏在哪里了……哦,是的RouteMatch,它们在里面,当然它们在那里,你不觉得吗?

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

要获得参数,我必须在我的BooController::booAction

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

5 回答 5

12

为什么不使用 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 [];
}
于 2013-12-07T19:27:52.117 回答
8

您可以创建一个容器类并在两个控制器中使用它

在 module.conf

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'my_handy_container'        => 'path\container_class_name',
        )
    );
}

在两个控制器中创建一个 getter:

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 传递变量的方法

在传递方法中:

return $this->forward()->dispatch('controller_name', [
    'action' => 'whatever',
    'varname' => $value,
    'varname2' => $value2
]);

在调用的控制器方法中,执行:

$param2 = $this->params()->fromRoute('varname2',false);
于 2012-12-08T10:35:49.040 回答
2

以为我会添加另一个适合我的选项。

您可以简单地通过 forward 函数直接传递参数,并使用 routeMatch 函数在另一端访问它们。

return $this->forward()
            ->dispatch('Module\Controller\Foo', array(
                                            'action' => 'bas', 
                                             'id' => 6)
                                              );

传递给Foo控制器,此方法中的basAction然后您可以使用以下代码访问 id 参数

$myParam = (int) $this->getEvent()->getRouteMatch()->getParam('id');

不确定这是否符合您的要求 - 但对我有用。

于 2014-02-05T21:51:48.640 回答
1

谢谢你的问题,对我帮助很大。找到了一种将所有参数传递给 forward()->dispatch(...) 的简单方法。在控制器的 action 方法中:

$params = $this->params()->fromRoute();

返回数组 $data 作为 $data 传递给 forward()->dispatch($controllerName, $data)。

于 2013-12-16T19:11:18.670 回答
1

在官方 ZF2 文档中,它是如何工作的:

$params是一个可选的参数数组,用于为 RouteMatch 对象播种以用于此特定请求。这意味着参数将通过它们的键与配置中的路由标识符匹配(否则将忽略不匹配的键)

所以像这样通过:

$params = array(
    'foo' => 'foo',
    'bar' => 'bar'
);
$this->forward()->dispatch('My\Controller', $params)

然后您可以My\Controller正常获取您的路线匹配参数:

$foo = $this->params()->fromRoute('foo');
$bar = $this->params()->fromRoute('bar');

对于那些在控制器中难以访问参数的人来说,这里有一份来自这个 CheatSheet的很好的概述。

$this->params()->fromPost('foo');  //POST
$this->params()->fromQuery('foo'); //GET
$this->params()->fromRoute('foo'); //RouteMatch
$this->params()->fromHeader('foo');//Header
$this->params()->fromFiles('foo'); //Uploaded file
于 2015-04-17T08:04:58.897 回答