zend framework2 - ZF2 - How to pass data from controller to helper -
i need pass data controller custom view helper. tried in 2 ways, unfortunately can't make working.
have registered helper in module.config.php
the first way tried pass variables controller helper:
in controller:
public function indexaction() { $this->data = $this->getapplicationtable()->gettypes(); $helper = new testhelper(); $helper->setvariables($this->data); }
here helper:
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 correct data } } }
in layout, display this:
<?php echo $this->testhelper(); ?>
and got error helper-view.phtml variable empty.
the second way tried based on dependency injection
my 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; } ), ); }
my 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; } }
i got same error in first way.
grateful kind of help.
it doesn't work, because call $this->testhelper(); create new instance of testhelper, not know $data, setvariables never called instance. not return view inside helper, leave if possible in controller. helper used give helper methods view, consult documentation: http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html#writing-custom-helpers
an easy solution, how without di: give desired data controller view, view can give data helper if required:
inside indexaction, set data variable , give variable view:
public function indexaction() { $data = $this->getapplicationtable()->gettypes(); return new \zend\view\model\viewmodel(array( 'data' => $data, )); }
change helper: remove setvariables method , add in signature of invoke $data param. btw, practice set variable private , add setters , getters.
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; } }
now, in index.phtml can $data controller , pass helper:
$data = $this->data; echo $this->testhelper($data);
that's all.
Comments
Post a Comment