ZF2 Share partial view (menu) betwen controllers of same module -
in zf2 have module more controllers, shares let menu (for navigating betwen controllers). share menu between controllers have put in every controller view:
<div class="span2"> <?php echo $this->partial('leftmenu'); ?> </div> <div class="span10"> controller view </div>
so repeating <?php echo $this->partial('leftmenu'); ?>
code everywhere. there way doing once , make appear in views of controllers?
one way wold inject viewmodel layout , have rendered in there.
module.php
/** * on bootstrap event * * @param \zend\mvc\mvcevent $e */ public function onbootstrap(mvcevent $e) { // .. $viewmodel = $e->getviewmodel(); $menu = new viewmodel(); $menu ->settemplate('module/menu/something'); $viewmodel->addchild($menu, 'menu'); }
you need render menu inside layout then..
layout.phtml
<?php if(isset($menu)) echo $menu ?>
you can alternatively attach onto different events if want different effects, such injecting module if you're in module, or controller extends abstract controller:
// bootstrap /// $e->getapplication()->geteventmanager()->getsharedmanager() ->attach('zend\mvc\controller\abstractactioncontroller', 'dispatch', function($e) { $controller = $e->gettarget(); $controllerclass = get_class($controller); $modulenamespace = substr($controllerclass, 0, strpos($controllerclass, '\\')); // logic decide want $viewmodel = $e->getviewmodel(); $menu = new viewmodel(); $menu ->settemplate('module/menu/something'); $viewmodel->addchild($menu, 'menu'); }, 100);
Comments
Post a Comment