7

我在控制器的操作方法,该方法需要参数:ZF2:如何将参数传递给转发插件,然后我可以将其转发给我的方法?

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

,我需要一些控制器的其他方法转发给该方法。其中一个参数必须通过参考参数。 the manual具有的唯一示例是:

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

但它不工作。

有什么办法可以将其他参数传递给转发控制器吗?

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后,我在我的嘘声嘘\控制器\ BooController的行动让他们从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的对象中。

- Tryyyy猜猜我们隐藏你的参数在哪里......哦,是的,他们在RouteMatch,当然他们在那里,你不觉得别的吗?

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

要获取参数,可以我必须做这在我BooController::booAction

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

是'Boo'有效的Controller-Alias?正如文档所指出的,Boo'需要是一个完全合格的ClassName(例如'Mymodule \ Controller \ BooController'),或者它应该是你在配置中定义的这个控制器的别名。 – Sam

+0

是的,它是一个完全合格的ClassName ,它是我的Boo \ Controller \ Boo''。我在这个问题中这样写道,以缩短它。现在改变以避免模糊。 – Green

+1

这仍然只是一个别名。假设你已经从'Boo \ Controller \ Boo' =>'Boo \ Controller \ BooController'设置了一个invokeable别名是安全的?除此之外,你的第一种方法是正确的。你如何测试参数是否存在?它们是否也在路由配置中定义? Tihs blogpost使用您正在寻找的功能:http://www.michaelgallego.fr/blog/2012/10/06/how-to-replace-the-action-helper-in-zf-2-and -make伟大,部件化的内容/ – Sam

回答

6

您可以创建一个容器类和module.conf

使用两个控制器

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

在两个控制器中创建一个吸气器:

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的方式来传递乏使用前

在该传送方法做:

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

在调用控制器的方法,这样做:

$param2 = $this->params()->fromRoute('varname2',false); 
10

为什么不使用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 []; 
} 
1

谢谢你的问题,对我帮助很大。找到一个简单的方法来获取传递给forward() - > dispatch(...)的所有参数。在控制器的动作方法:

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

返回数组$数据作为传递作为$数据到向前() - >调度($ controllerName,$数据)。

2

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

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

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

传递给控制器,basAction在这个方法中,你可以使用下面的代码访问ID PARAM

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

不知道这是否符合您的要求 - 但工程我。

1

Here in the official ZF2 documentation被写入它是如何工作的:

$params是与种子一个RouteMatch对象为这一特定的请求的目的的参数可选阵列。这意味着这些参数将被它们的密钥匹配到配置中的路由标识符(否则忽略不匹配的密钥)

所以通过这样的:

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

然后你就可以在路线匹配PARAMS您My\Controller像正常:

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

对于人们在这里访问他们的控制器内的参数一个很好的挣扎概览从this 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 
相关问题