Zend Framework自动模块路由

时间:2012-11-20 16:37:13

标签: zend-framework

我的zend框架有以下结构:

application/
    configs/
        application.ini
        routes.php
    layouts/
        scripts/
            includes/
            layout.phtml
    modules/
        somemodule1/
            controllers/
                IndexController.php
                ErrorController.php
            models/
            views/
                helpers/
                scripts/
                    error/
                        error.phtml
                    index/
                        index.phtml
            forms/
            Bootstrap.php
        somemodule2/
            controllers/
                IndexController.php
                ErrorController.php
            models/
            views/
                helpers/
                scripts/
                    error/
                        error.phtml
                    index/
                        index.phtml
            forms/
            Bootstrap.php

    Bootstrap.php
library/
    Zend/
    CustomClasses/
public/
    css/
    images/
    js/
    uploads/
    .htaccess
    index.php

我试图让它变得模块化。例如,我想要达到这样的每个模块: domain.com/modulename/controller/action/someotherparams。如果我添加新模块,那么它应该自动加载而不对路由类进行任何更改。我已经做的是:

**routes.php**

protected function _initAutoloadModules()
{
    $autoloader = new Zend_Application_Module_Autoloader(
        array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH . '/modules/somemodule1'
        ), 
        array(
            'namespace' => 'Admin',
            'basePath'  => APPLICATION_PATH . '/modules/somemodule2'
        )            
      );
    return $autoloader;
}

**application.ini** 

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
includePaths.library = APPLICATION_PATH "/../library"
appnamespace = "Default"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.defaultModule = "default"
resources.frontController.defaultController = "index"
resources.frontController.defaultAction = "index"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"
resources.modules = ""
resources.view[] =
resources.session.remember_me_seconds = 864000
resources.session.use_only_cookies = on
includePaths.models = APPLICATION_PATH "/models/"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

但实际上它不按我想要的方式工作。因为我无法在不更改routes.php文件的情况下自动加载模块(在将新模块添加到系统之后)。那么有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您希望实现的行为是Zend Framework的默认行为。试试这种方式:

 $moduleLoader = new Zend_Application_Module_Autoloader
    (
            array
            (
                'namespace' => '',
                'basePath' => APPLICATION_PATH
            )
        );

不要忘记在控制器名称中使用模块前缀:某些module1 IndexController。