zend framework2 - How to pass variables to layout.phtml globally in ZF2? -


i want pass series of variables layout.phtml throughout whole application(globally). , mean don't wanna use

$this->layout()->somevar = somevalue; 

in each , every action i've got, since lot of work , code. there way in 1 place? or mentioned got! hope not :)

maybe using sessions ? – remi thomas

thanks solution. time being that's i'm using. logged-in user info, system , layout settings , acl list. problem have define new object in layout.phtml don't think appropriate, it? read somewhere whatever data need use in view models should passed using controller actions. , specially i'm not fan of cutting corners, if there's clean way i'd rather not way. , have number of unread messages each user , use in layout.phtml. if in layout.phtml it's lot of php script inside view model or layout.

thanks

the best , cleanest way using viewhelper, orochi suggests. here instructions create own viewhelper: http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html. it's not complex ;)

however problem resolved in way. supposing variables need contain values provided services present in application (and exposed zf2 servicemanager), add lines on "onboostrap" function inside module.php (e.g. on application module).

here example:

public function onbootstrap($e) {      $servicemanager = $e->getapplication()->getservicemanager();     $viewmodel = $e->getapplication()->getmvcevent()->getviewmodel();      $myservice = $servicemanager->get('mymodule\service\myservice');      $viewmodel->somevar = $myservice->getsomevalue();  } 

in way write assignment in 1 place. variable accessible usual:

$this->layout()->somevar; 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -