Zend Framework Source Code Snippets

Dynamic Module-based Layout Picker

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
<?php
 
class Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract 
{
        /**         * preDispatch function.
         * 
         * Define the layout path based on what module is being used.
         *
         * @author Aaron (bachya1208[at]googlemail.com)         * @access public
         * @param Zend_Controller_Request_Abstract $request
         * @return void
         */
        public function preDispatch(Zend_Controller_Request_Abstract $request)        {
                $module = strtolower($request->getModuleName());
                $layout = Zend_Layout::getMvcInstance();
                                
                if ($layout->getMvcEnabled())                {
                        $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts/');
                        $layout->setLayout($module);
                }
        }}
 
## 
In bootstrap.php -
 <?php
    defined('APPLICATION_PATH')
        or define('APPLICATION_PATH', dirname(__FILE__));
 
    ... 
    Zend_Layout::startMvc();
    $frontController->registerPlugin(new Plugin_ModuleLayout()); 
?>

Comments

Olagato Olagato
2 years ago

beautiful !

davedevelopment davedevelopment
2 years ago

How about a fall back to the default module layout in case their isn't a specific module layout?

Fábio Fábio
2 years ago

Great!

But, what about controller/models.views?

I'm looking for a Plugin that handles with everything to put models to work

bachya bachya
2 years ago

@davedevelopment: good idea - I'm kind of relying on people actually using my proposed directory structure. I'll make updates soon and repost.

@Fabio: I'm not sure I understand your question - you're looking for a directory structure where each module has its own controllers, models, and views?

Snowcore Snowcore
2 years ago

Oh, this is what I really need!

Slack5 Slack5
2 years ago

couldn't you simply check if a layout file exists then setlayout?
I am currently only using 3 layouts (login, admin, public) and I simply setlayout for the login module (default is set in bootstrap).

I could see how (for example) a shop could use muliple layouts for index, category, product pages.

just my 2 cents

alessiofx alessiofx
2 years ago

how do I make this with the 'latest version of the bootstrap ZendFrameworkQuickstart-20090430 ???

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Assuming a directory structure where each module has its own layout folder:

EXAMPLE:

/application/modules/default/layouts
/application/modules/admin/layouts
...

This plugin will determine which layouts folder to use based on which module is currently in use. Probably could be expanded/tightened up, so I'd love to hear your comments!

Snippet details

Created:
bachya bachya
2 years ago
Edited:
bachya bachya
2 years ago
Revision Id:
33
Edit Message:
Initial Release
Tags:
module layout plugin
Comments:
7
Views:
2613
Points:
0 (0 votes)

History

r33

Initial Release

bachya bachya
2 years ago