I've seen that trying to load different layouts for different modules is
quite a big topic of discussion on here. I just managed to get it working
and thought I'd just share the way I did it.
First things first, my application structure is set out as follows:
my/
--- application/
--------------- modules/
------------------------ main/
------------------------------ controllers/
------------------------------ views/
------------------------------ layouts/
-------------------------------------- main.phtml
------------------------ admin/
------------------------------ controllers/
------------------------------ views/
------------------------------ layouts/
-------------------------------------- admin.phtml
------------------------ default/
------------------------------ layouts/
-------------------------------------- default.phtml
The default layout is just there as a fallback. I don't have any controllers
in the default module because I have set the default module to "main" in my
config file.
The first thing to do is to set up the default layout settings and at the
same time get an instance of Zend_layout using the layout application
resource that comes with 1.8. To do this we only need to the following lines
to the application.ini:
resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/modules/default/layouts"
Assuming you have the file default.phtml in /modules/default/layouts with
some distinguishable layout then your site should now be using it.
Now, most people have been trying to use module bootstraps to load layouts
for each module but this is totally unneccessary.
All we have to do is create a new controller plugin that deals with the
request object and sets the layout accordingly. This is what I did (Note: I
haven't added any code that checks the validity of the request yet so
presumably if the wrong address is typed no layout will be loaded):
class My_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract
$abstract)
{
$layout = Zend_Layout::getMvcInstance();
$layout->setLayout($request->getModuleName())
->setLayoutPath(APPLICATION_PATH . '/modules/' .
$request->getModuleName() . '/layouts');
}
}
All I've done here is defined which layout to use based on the module name
in the controller request object at dispatchLoopStartup().
All that is left to do is register the plugin with the front controller.
Most of you probably know how to do this but just in case - in your
application bootstrap:
protected function _initAutoLoader()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My');
$autoloader->suppressNotFoundWarnings(false);
}
protected function _initPlugins()
{
$bootstrap = $this->getApplication();
if ($bootstrap instanceof Zend_Application) {
$bootstrap = $this;
}
$bootstrap->bootstrap('FrontController');
$front = $bootstrap->getResource('FrontController');
$plugin = new My_Controller_Plugin_Layout();
$front->registerPlugin($plugin);
}
The first method sets up the project namespace so that the application knows
to look in library/my/..... for resources and the initPlugins() method
registers the plugin.
Hope this helps. Any questions do not hesitate to ask.
Matt
--
View this message in context:
http://www.nabble.com/Quick-Guide-How-to-use-different-Layouts-for-each-module-tp23443422p23443422.html
Sent from the Zend Framework mailing list archive at Nabble.com.