【问题标题】:Using multiple controller directories with Zend Framework在 Zend Framework 中使用多个控制器目录
【发布时间】:2011-12-06 19:37:22
【问题描述】:

我正在使用 Zend Framework 构建一个 CMS,我的主题可以支持自定义布局、视图和控制器。

如果主题有自定义布局,我将布局目录设置为主题目录。如果主题使用自定义视图,我将视图目录设置为主题目录。

但我坚持使用控制器,因为我想使用我的默认控制器作为后备。一个主题只有它的自定义控制器,而不是每个控制器。例如,如果它支持 Image Gallery 功能,则必须有 ImageGalleryController.php 文件,但不能有 IndexController 或 ErrorController 控制器。

总结:如果主题文件夹中存在控制器,则主题可以覆盖控制器,否则使用默认值。

我该怎么做?谢谢。

【问题讨论】:

  • 你考虑过使用路由吗?
  • 我认为这与路线无关。

标签: php model-view-controller content-management-system zend-framework


【解决方案1】:

我不确定我是否完全理解,但我认为您可能正在寻找的东西是模块? 例如

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)

然后您只需创建一个不错的插件,然后切换布局。像这样的:

<?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 );
        }
    }

}

【讨论】:

  • 感谢您的努力,但正如我在问题中已经说过的那样,我可以做到这一点。布局和视图没有问题。我需要像你一样的东西,但对于控制器。
  • 那会违背整个 MVC 模型吗?认为使用 Zend 组件构建自己的框架可能会更容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多