67832

How do I pass variables to a layout in a multiple module / layout Zend Framework application?

Question:

Before I converted my Zend Framework application to a module based structure, it had a single layout and I could pass variables to it from my controller like so:

// Controller action $this->view->foo = 'Something'; // Layout <?= $this->foo ?>

However, since moving everything into a default module and creating a separate "admin" module, I can no longer get this to work, most likely due to me moving my "View Settings" out of my bootstrap file and into the controller plugin where I am switching the layout based on the module. My plugin looks like this:

class KW_Controller_Plugin_LayoutSelector extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $layout = Zend_Layout::getMvcInstance(); $layout->setLayout($request->getModuleName()); $view = $layout->getView(); $view->doctype('HTML5'); $view->setEncoding('UTF-8'); $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8'); $view->headScript()->appendFile('/js/jquery-1.7.1.min.js'); switch ($request->getModuleName()) { case 'admin': $view->headTitle('Admin Area')->setSeparator(' - '); $view->headLink()->appendStylesheet('/css/admin/global.css'); $view->headScript()->appendFile('/js/admin/common.js'); break; default: $view->headTitle('Main Site')->setSeparator(' - '); $view->headLink()->appendStylesheet('/css/global.css'); $view->headScript()->appendFile('/js/common.js'); break; } } }

If I move all of those method calls to the view back into my bootstrap, I can pass variables to the layout again; so I'm guessing that something is happening in the wrong order, maybe my layout is being switched after I have passed the variables to the view from my controller and they're not making it into my layout? (I've tried changing the point at which the above runs by putting the code in both preDispatch() and postDispatch(), etc.)

It's worth noting, that I can access these variables in my individual view scripts, just not the layout that they're contained within.

Any pointers would be greatly appreciated.

Answer1:

I'm doing something similar in a current project and it's largely working fine.

One difference I note is that I am bootstrapping both Layout and View at the app-level using the standard application resources.

Then in my plugin, I access the view using:

$front = Zend_Controller_Front::getInstance(); $view = $front->getParam('bootstrap')->getResource('view');

This seems to guarantee I am accessing the same view instance at bootstrap, in plugins, and in controllers.

Answer2:

Maybe this will not solve the whole problem but it will help a bit with this ugly swith You have there :-)

public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); $layout = Zend_Layout::getMvcInstance(); // check module and automatically set layout $layoutsDir = $layout->getLayoutPath(); // check if module layout exists else use default if(file_exists($layoutsDir . DIRECTORY_SEPARATOR . $module . ".phtml")) { $layout->setLayout($module); } else { $layout->setLayout("layout"); } }

}

Recommend

  • How to compare input from process.stdin to string in NodeJS?
  • Nodejs connect EMFILE - How to reuse connections?
  • while(count!==3) fail to check callback state
  • Node Js access to api laravel
  • How do I pass variables to a layout in a multiple module / layout Zend Framework application?
  • Printer ZebraZ4MPlus don't print Russian Cirillyc character
  • Node.js HTTP request returns 2 chunks (data bodies)
  • how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding?
  • Zend Framework: Get whole out output in postDispath while using Layout
  • Zend overwrite default view object
  • Using sfFilter to update DB with Doctrine
  • Display some part of text in bold and some part in italic in SWT label
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • How to add id attribute to Zend Framework form?
  • How to store items of multiple JSON Array and display it listview?
  • Change Background on a specific row based on a condition in Custom Adapter
  • Bootstrap 4: Why popover inside scrollable dropdown doesn't show?
  • GridBagLayout padding
  • Is there any way to center certain columns in table?
  • Listview with multiple strings
  • Overriding window.location on Webbrowser Control
  • Declaration of does not work with Ionic's ion-nav-view in IBM MobileFirst
  • Is the listview recycling early? Or is Universal ImageLoader canceling in error? Or is my Logic wron
  • How to read JSON-LD data from HTML in Objective-C?
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • How to add regEx in angular filter
  • Need a consistent TimePicker gizmo for mobile web site
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Jquery UI tool tip close icon
  • SSO with signing and signature validation doesn't work
  • JTable with a ScrollPane misbehaving
  • costura.fody for a dll that references another dll