问题
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.
回答1:
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.
回答2:
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");
}
}
}
来源:https://stackoverflow.com/questions/10074671/how-do-i-pass-variables-to-a-layout-in-a-multiple-module-layout-zend-framework