ZF2 - 如何将数据从控制器传递给帮助器

时间:2013-07-22 10:06:15

标签: zend-framework2 helper

我需要将数据从控制器传递到我的自定义视图助手。我试图以两种方式做到这一点,但遗憾的是我无法使其发挥作用 我在module.config.php

中注册了帮助器

第一种方式我试图将变量从控制器传递给助手:
在我的控制器中:

public function indexAction()
{  
        $this->data = $this->getApplicationTable()->getTypes();

        $helper = new TestHelper();
        $helper->setVariables($this->data);
}

这是我的帮手:

class TestHelper extends AbstractHelper {

    public $data;


    public function __invoke()
    {
        var_dump($this->data); // output null
        return $this->getView()->render('helper-view.phtml', $this->data);
    }



    public function setVariables($var)
    {
        if($var){
            $this->data = $var;
            var_dump($this->data) // output array with correct data
        }
    }



}

在布局中,我这样显示:

<?php echo $this->testHelper(); ?>

我从helper-view.phtml得到错误,该变量为空。

我试过的第二种方式基于依赖注入

我的module.php:

public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Application\Model\ApplicationTable' =>  function($sm) {
                    $tableGateway = $sm->get('ApplicationTableGateway');
                    $table = new ApplicationTable($tableGateway);
                    return $table;
                },
                'ApplicationTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new TableGateway('tableName', $dbAdapter);
                },
            ),
        );
    }

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'TestHelper' => function ($helperPluginManager) {
                $sm = $helperPluginManager->getServiceLocator();
                $tableGateway = $sm->get('Application\Model\ApplicationTable');
                $viewHelper = new TestHelper();
                $viewHelper->setTableGateway($tableGateway);
                return $viewHelper;
            }
        ),
    );
}

我的助手:

class TestHelper extends AbstractHelper {

    public $tableGateway;


    public function __invoke()
    {
        $data = $this->tableGateway->getTypes();

        return $this->getView()->render('helper-view.phtml', $data);
    }



    public function setTableGateway($tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }


}

我得到了与第一种方式相同的错误 我会感激任何帮助。

2 个答案:

答案 0 :(得分:1)

它不起作用,因为调用$ this-&gt; testHelper();将创建一个新的TestHelper实例,它不知道$ data,从未为此实例调用setVariables。 此外,我不会返回帮助程序内的视图,如果可能,请在控制器中保留此视图。帮助程序用于为视图提供帮助程序方法,请参阅文档:http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html#writing-custom-helpers

一个简单的解决方案,如何在没有DI的情况下执行此操作:将所需数据从控制器提供给视图,以便视图可以在需要时将数据提供给帮助程序:

在indexAction中,设置数据变量并将变量赋予视图:

public function indexAction() {
    $data = $this->getApplicationTable()->getTypes();
    return new \Zend\View\Model\ViewModel(array(
        'data' => $data,
    ));
}

更改你的助手:删除setVariables方法并添加调用$ data param的签名。顺便说一句,最好将变量设置为private并添加setter和getter。

class TestHelper extends AbstractHelper {

    private $_data;

    public function __invoke($data) {
        $this->setData($data);
        return var_dump($this->getData());
    }

    public function setData($data) {
        $this->_data = $data;
    }

    public function getData() {
        return $this->_data;
    }

}

现在,在index.phtml中,您可以从控制器获取$ data并将其传递给帮助程序:

$data = $this->data;
echo $this->testHelper($data);

这就是全部。

答案 1 :(得分:0)

如果要使用依赖项注入执行此操作,则需要在构造期间将tableGateway作为参数传递。否则,您可以在调用期间传递它。对于后者,如果可以,请执行以下操作:

 class TestHelper extends AbstractHelper {

     public function __invoke($tableGateway)
     {
         $data = $tableGateway->getTypes();

         return $this->getView()->render('helper-view.phtml', $data);
     }

并在布局中:

  

<?php echo $this->testHelper($tableGateway); ?>

或者,对于通过构造函数插入它的方法:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'TestHelper' => function ($helperPluginManager) {
                $sm = $helperPluginManager->getServiceLocator();
                $tableGateway = $sm->get('Application\Model\ApplicationTable');
                $viewHelper = new TestHelper($tableGateway);
                return $viewHelper;
            }
        ),
    );
}

class TestHelper extends AbstractHelper {
    protected $tableGateway;        

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

    public function __invoke()
    {
        $data = $this->tableGateway->getTypes();
        return $this->getView()->render('helper-view.phtml', $data);
    }

}

并在布局中:

  

<?php echo $this->testHelper(); ?>

我没有足够的信息或足够的代码来验证这在您的情况下是否有效,但您可以查看http://zendblog.shinymayhem.com/2013/09/using-servicemanager-as-inversion-of.html以获取有关工厂和依赖项注入的更多信息。

相关问题