简体   繁体   中英

Using multiple controller directories with Zend Framework

I'm building a CMS with Zend Framework and my themes can support custom layout, view and controllers.

If a theme has a custom layout i set the layout directory to theme directory. If a theme uses custom views i set the views directory to theme directory.

But i'm stuck with contollers becase i want to use my default controllers as fallback. A theme will only have it's custom controllers not every controller. For example if it supports Image Gallery feature there must be a ImageGalleryController.php file but not IndexController or ErrorController controllers.

To sum it up: A theme can override controllers if a controller exists in theme folder use it, else use the default.

How can i do this? Thanks.

I am not sure I fully understand, but I think the thing you might be looking for is modules? Eg

Application/
    Configs/
    Modules/
        default/
            controllers/
            layouts/
            other standard folders (like views, models etc)
        othermodule/
            controllers/
            layouts/
            other standard folders (like views, models etc)
        etc/
            controllers/
            layouts/
            other standard folders (like views, models etc)

Then you just create a nice plugin, and switch the layouts. Something like this:

<?php
/**
 * This class will change the layout per modular basis. If a layout is not found, it will use the specified modular layout.
 *
 * @author Elliott Websites
 */
class App_Modular_layout extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch( Zend_Controller_Request_Abstract $request )
    {
        $module = $request->getModuleName();
        $layout = Zend_Layout::getMvcInstance();

        $defaultLayout = APPLICATION_PATH . '/modules/default/layouts';
        $defaultLayoutName = 'default';

        if( file_exists( APPLICATION_PATH . '/modules/' . $module . '/layouts/' . $module . '.phtml' ) )
        {
            $layout->setLayoutPath( APPLICATION_PATH . '/modules/' . $module . '/layouts' )
                           -> setLayout( $module );
        } else {
            $layout->setLayoutPath( $defaultLayout )
                           ->setLayout( $defaultLayoutName );
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM