Zend framework2 ZF2-如何将数据从控制器传递给助手
我需要将数据从控制器传递到我的自定义视图助手。我试着用两种方法来做,但不幸的是,我无法让它工作。Zend framework2 ZF2-如何将数据从控制器传递给助手,zend-framework2,helper,Zend Framework2,Helper,我需要将数据从控制器传递到我的自定义视图助手。我试着用两种方法来做,但不幸的是,我无法让它工作。 我已经在module.config.php中注册了helper 第一种方法我试图将变量从控制器传递给助手: 在我的控制器中: public function indexAction() { $this->data = $this->getApplicationTable()->getTypes(); $helper = new TestHelp
我已经在module.config.php中注册了helper 第一种方法我试图将变量从控制器传递给助手:
在我的控制器中:
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(); ?>
我的助手:
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;
}
}
我的错误与第一种方法相同。我将非常感谢任何帮助。它不起作用,因为调用$this->testHelper();将创建TestHelper的一个新实例,该实例不知道$data,但从未为此实例调用过setVariables。 此外,我不会在助手中返回视图,如果可能,请将其保留在控制器中。帮助器用于为视图提供帮助器方法,请参阅文档: 一个简单的解决方案,如何在没有DI的情况下做到这一点:将所需的数据从控制器提供给视图,以便视图可以在需要时将数据提供给助手: 在indexAction中,设置数据变量并将变量提供给视图:
public function indexAction() {
$data = $this->getApplicationTable()->getTypes();
return new \Zend\View\Model\ViewModel(array(
'data' => $data,
));
}
更改助手:删除setVariables方法并添加invoke的签名$data参数。顺便说一句,将变量设置为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);
仅此而已。如果要使用依赖项注入实现这一点,则需要在构造期间将tableGateway作为参数传递。否则,您可以在调用期间传递它。对于后者,如果可能,请执行以下操作:
class TestHelper extends AbstractHelper {
public function __invoke($tableGateway)
{
$data = $tableGateway->getTypes();
return $this->getView()->render('helper-view.phtml', $data);
}
在布局中:
或者,对于通过构造函数插入的方法:
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;
}
),
);
}
及
在布局中:
我没有足够的信息或您的代码来验证这在您的情况下是否有效,但是您可以查看有关工厂和依赖项注入的更多信息 感谢您的回答,不幸的是我没有在这个操作中使用view(index.phtml)。这是一种没有内容的大地图,这就是为什么我不能从视图中传递它,在索引上到底应该发生什么?如果你给我一个解释,我会相应地更新答案。我需要使用助手在布局中显示复选框(看起来像地图)。要做到这一点,我需要从数据库中获取信息并将其传递给助手。所以您有一个未连接到控制器的视图输出?对我来说,这听起来像是没有遵循MVC原则,或者我只是不明白你的意图。用信息更新问题可能会更好,这些信息会显示如何加载地图视图以及何时或在什么情况下需要显示复选框。否则很难给出“正确”的解决方案。“helper-view.phtml中的错误,该变量为空。”哪个变量为空?
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);
}
}