如何将参数从View Helper(小部件)传递给action?

时间:2014-12-10 10:48:00

标签: zend-framework2 zend-view

我有一个小部件,它正在为另一个控制器调用一个动作,我需要将一个参数传递给动作。到目前为止我尝试了以下内容,我收到以下错误:

错误

Fatal error: Call to a member function getParam() on a non-object in C:\dev\projects\OnlineFieldEvaluation\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\Plugin\Params.php on line 118

的widget     

namespace OnlineFieldEvaluation\View\Helper;

use OnlineFieldEvaluation\Controller\TabsController;
use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;


class IdentityInformationWidget extends AbstractHelper implements ServiceLocatorAwareInterface
{

    protected $idinfoService = null;

    public function __construct(TabsController $idinfoService)
    {
        $this->idinfoService = $idinfoService;
    }

...

    /**
     * @param $id
     */
    public function __invoke($id)
    {

        $viewModel = $this->idinfoService->editidentityinformationAction($id);

        return $this->getView()->render($viewModel);

    }

}

控制器:

   public function editidentityinformationAction()
    {
      $id = (int)$this->params()->fromRoute('id', 0);
     //$id = (int)$this->params('id', 0);

...

        $view = new ViewModel(array(
            'id' => $id,
            'form' => $form,
        ));
        $view->setTemplate('online-field-evaluation/tabs/editidentityinformation.phtml');

        return $view;
    }

从视图中调用

 <?php echo $this->identityInformationWidget(3); ?>
尝试后,

编辑1::“$id = $this->getEvent()->getRouteMatch()->getParam('id', 0);

事件:

enter image description here

和“$this->getEvent()->getRouteMatch()”返回null

我正在尝试为我的用例修改此示例: http://www.michaelgallego.fr/blog/2012/10/06/how-to-replace-the-action-helper-in-zf-2-and-make-great-widgetized-content/

2 个答案:

答案 0 :(得分:0)

尝试:     $ this-&gt; getEvent() - &gt; getRouteMatch() - &gt; getParam(&#39; id&#39;,0);

答案 1 :(得分:0)

有几种方法可以实现您的小部件:

首先只是在你尝试做的时候调用你想要的动作,在你的情况下将params传递给你的动作(比如$ id)但是这样,你的动作可能无法按预期工作,因为你正在创建一个“新的”条目指向你的行动。你可以这样面对麻烦。

我认为最好的方法是使用forward() - &gt; dispatch控制器插件,因为ZF2将获取所有上下文并隔离处理以提供您可以渲染的ViewModel实例。你将获得更好的隔离。

例如:

注意:您的路由定义必须是Segment类型,并且路由模式中包含:id

控制器

public function editidentityinformationAction()
    {
      // use params form routing
      $id = (int)$this->params()->fromRoute('id', 0);

      // Some Logic using dispatch context, servicemanager, etc...
...

        $view = new ViewModel(array(
            'id' => $id,
            'form' => $form,
        ));
        $view->setTemplate('online-field-evaluation/tabs/editidentityinformation.phtml');

        return $view;
    }

查看助手:

class IdentityInformationWidget extends AbstractHelper implements ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    public function __construct(ServiceLocatorInterface $serviceLocator)
    {
        // $serviceLocator is an instance of ViewHelperManager
        // We get ServiceManager (its "parent" ServiceLocatorInterface) with $serviceLocator->getServiceLocator()
        // Make good use of your ServiceLocatorAwareInterface
        $this->setServiceLocator($serviceLocator->getServiceLocator());
    }

...

    /**
     * @param $id
     */
    public function __invoke($id)
    {
        // Going through ServiceManager to get ControllerPluginManager and Forward controller plugin
        $forwardPlugin = $this->getServiceLocator()
             ->get('ControllerPluginManager')->get('forward');

        // Dispatch Action with parameter (Force ZF2 MVC dispatch stack process)
        $viewModel = $this->forward()
           ->dispatch('Onlin‌​eFieldEvaluation\Controller\TabsController', array(
               'action' => 'editidentityinformation',
               'id'     => $id
           ));

        return $this->getView()->render($viewModel);

    }

}
  • 注意:我没有测试这段代码,肯定存在错误,但逻辑就在这里

http://framework.zend.com/manual/2.3/en/modules/zend.mvc.plugins.html#forward-plugin有关前向插件的更多帮助

通过这种方式,您可以将您的操作作为普通页面,或者在视图帮助下作为窗口小部件,您的操作也是可测试的,因为它的逻辑不会像网页或窗口小部件那样更改,您还可以测试你可以轻松地模拟你的小部件,等等......