php - Zend 2: Where should I put this method that updates a module's nav bar -


i previously asked question options available inject navbar (template) automatically on per module basis.

following 1 of suggestions added view variable in myapp/module/mymodule/module.php path of template

public function onbootstrap($e) {     $viewmodel = $e->getapplication()->getmvcevent()->getviewmodel();     $viewmodel->subnav = 'mymodule/mymodule/subnav'; } 

then in application/view/layout/layout.phtml checked presence of variable , displayed it:

        <?php if($this->subnav) : ?>         <nav class="navbar navbar-inverse" role="navigation">             <?php echo $this->partial($this->subnav); ?>         </nav>         <?php endif ?> 

this works want to, want pass template dynamic data database.

so question this: ok in module.php right? in onbootstrap() want call data model access db , store results in view variable. misuse of module.php? kind of operation supposed done in controller, want template injected automatically without controller having know it. i'm new mvc , zend , want make sure i'm not violated fundamental design principle.

create custom view helper , load data in , render view , use in layout

namespace application\view\helper; use zend\view\helper\abstracthelper;  class mynav extends abstracthelper{      public function __invoke(){         //load data          return $this->view->render('my nav view script',array('data'=>$data))     } } 

in modules config file

'view_helpers' => array(     'invokables' => array(         'mynav'  => 'application\view\helper\mynav',     ),   ), 

in layout

$this->mynav(); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -