Мне нужно передать данные от контроллера в свой пользовательский помощник. Я пытался сделать это двумя способами, но, к сожалению, я не могу заставить его работать.
Я зарегистрировал помощник в module.config.phpZF2 - Как передать данные от контроллера к помощнику
Первый способ я пытался передать переменные из контроллера помощника:
В моем контроллере:
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;
}
),
);
}
Мой Helper:
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;
}
}
я получил такую же ошибку, как и в первом способе ,
Буду признателен за любую помощь.
«ошибка из helper-view.phtml, эта переменная пуста». Какая переменная пуста? – rwilson04