Zend Framework:在 config.ini 中自动加载模块资源?

发布于 2024-08-25 00:24:30 字数 635 浏览 3 评论 0原文

是否可以在 application.ini 中配置以下行为?

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{
    protected function _initAdminModuleAutoloader()
    {

    $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Admin',
        'basePath'  => APPLICATION_PATH . '/modules/admin',
    ));
        $this->_resourceLoader->addResourceTypes(array(
            'model' => array(
                'namespace' => 'Model',
                'path'      => 'models'
            )
        ));
}

}
?>

如果是这样,您能给我们举个例子吗?

谢谢。

Is it possible, to configure the following behaviour in application.ini?

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{
    protected function _initAdminModuleAutoloader()
    {

    $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Admin',
        'basePath'  => APPLICATION_PATH . '/modules/admin',
    ));
        $this->_resourceLoader->addResourceTypes(array(
            'model' => array(
                'namespace' => 'Model',
                'path'      => 'models'
            )
        ));
}

}
?>

If so, can you please show us an example?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-09-01 00:24:30

这里有一些提示。首先,您的 application.ini 文件中应包含以下行。

resources.modules.admin = "enabled"

这将确保 Zend_Application_Resource_Modules 中的以下函数运行

public function init()
{
    $bootstrap = $this->getBootstrap();
    $bootstrap->bootstrap('FrontController');
    $front = $bootstrap->getResource('FrontController');

    $modules = $front->getControllerDirectory();
    $default = $front->getDefaultModule();
    $curBootstrapClass = get_class($bootstrap);
    foreach ($modules as $module => $moduleDirectory) {
        $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
        if (!class_exists($bootstrapClass, false)) {
            $bootstrapPath  = dirname($moduleDirectory) . '/Bootstrap.php';
            if (file_exists($bootstrapPath)) {
                $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
                include_once $bootstrapPath;
                if (($default != $module)
                    && !class_exists($bootstrapClass, false)
                ) {
                    throw new Zend_Application_Resource_Exception(sprintf(
                        $eMsgTpl, $module, $bootstrapClass
                    ));
                } elseif ($default == $module) {
                    if (!class_exists($bootstrapClass, false)) {
                        $bootstrapClass = 'Bootstrap';
                        if (!class_exists($bootstrapClass, false)) {
                            throw new Zend_Application_Resource_Exception(sprintf(
                                $eMsgTpl, $module, $bootstrapClass
                            ));
                        }
                    }
                }
            } else {
                continue;
            }
        }

        if ($bootstrapClass == $curBootstrapClass) {
            // If the found bootstrap class matches the one calling this
            // resource, don't re-execute.
            continue;
        }

        $moduleBootstrap = new $bootstrapClass($bootstrap);
        $moduleBootstrap->bootstrap();
        $this->_bootstraps[$module] = $moduleBootstrap;
    }

    return $this->_bootstraps;
}

在上面的函数中,您的模块引导程序被调用。您需要在 /application/modules/admin 中有一个名为 Bootstrap.php 的模块引导文件,其中包含以下代码:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{


}

我将跳过几个步骤,但如果您跟踪继承类,这将导致以下函数在 Zend_Application_Module_Autoloader 中调用

public function initDefaultResourceTypes()
{
    $basePath = $this->getBasePath();
    $this->addResourceTypes(array(
        'dbtable' => array(
            'namespace' => 'Model_DbTable',
            'path'      => 'models/DbTable',
        ),
        'mappers' => array(
            'namespace' => 'Model_Mapper',
            'path'      => 'models/mappers',
        ),
        'form'    => array(
            'namespace' => 'Form',
            'path'      => 'forms',
        ),
        'model'   => array(
            'namespace' => 'Model',
            'path'      => 'models',
        ),
        'plugin'  => array(
            'namespace' => 'Plugin',
            'path'      => 'plugins',
        ),
        'service' => array(
            'namespace' => 'Service',
            'path'      => 'services',
        ),
        'viewhelper' => array(
            'namespace' => 'View_Helper',
            'path'      => 'views/helpers',
        ),
        'viewfilter' => array(
            'namespace' => 'View_Filter',
            'path'      => 'views/filters',
        ),
    ));
    $this->setDefaultResourceType('model');
}

对于您遵循此模式的每个模块,默认情况下将涵盖所有上述资源类型。您需要的任何其他内容都需要在引导文件中进行自定义。

Here are a couple of pointers. First you should have the below line in your application.ini file.

resources.modules.admin = "enabled"

That will make sure that the following function in Zend_Application_Resource_Modules runs

public function init()
{
    $bootstrap = $this->getBootstrap();
    $bootstrap->bootstrap('FrontController');
    $front = $bootstrap->getResource('FrontController');

    $modules = $front->getControllerDirectory();
    $default = $front->getDefaultModule();
    $curBootstrapClass = get_class($bootstrap);
    foreach ($modules as $module => $moduleDirectory) {
        $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
        if (!class_exists($bootstrapClass, false)) {
            $bootstrapPath  = dirname($moduleDirectory) . '/Bootstrap.php';
            if (file_exists($bootstrapPath)) {
                $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
                include_once $bootstrapPath;
                if (($default != $module)
                    && !class_exists($bootstrapClass, false)
                ) {
                    throw new Zend_Application_Resource_Exception(sprintf(
                        $eMsgTpl, $module, $bootstrapClass
                    ));
                } elseif ($default == $module) {
                    if (!class_exists($bootstrapClass, false)) {
                        $bootstrapClass = 'Bootstrap';
                        if (!class_exists($bootstrapClass, false)) {
                            throw new Zend_Application_Resource_Exception(sprintf(
                                $eMsgTpl, $module, $bootstrapClass
                            ));
                        }
                    }
                }
            } else {
                continue;
            }
        }

        if ($bootstrapClass == $curBootstrapClass) {
            // If the found bootstrap class matches the one calling this
            // resource, don't re-execute.
            continue;
        }

        $moduleBootstrap = new $bootstrapClass($bootstrap);
        $moduleBootstrap->bootstrap();
        $this->_bootstraps[$module] = $moduleBootstrap;
    }

    return $this->_bootstraps;
}

In the above function your module bootstrap is called. You need to have a module bootstrap file in /application/modules/admin called Bootstrap.php with the following code in it:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{


}

I'm going to skip a few steps but if you trace through the inheritance classes this will lead to the following function being called in Zend_Application_Module_Autoloader

public function initDefaultResourceTypes()
{
    $basePath = $this->getBasePath();
    $this->addResourceTypes(array(
        'dbtable' => array(
            'namespace' => 'Model_DbTable',
            'path'      => 'models/DbTable',
        ),
        'mappers' => array(
            'namespace' => 'Model_Mapper',
            'path'      => 'models/mappers',
        ),
        'form'    => array(
            'namespace' => 'Form',
            'path'      => 'forms',
        ),
        'model'   => array(
            'namespace' => 'Model',
            'path'      => 'models',
        ),
        'plugin'  => array(
            'namespace' => 'Plugin',
            'path'      => 'plugins',
        ),
        'service' => array(
            'namespace' => 'Service',
            'path'      => 'services',
        ),
        'viewhelper' => array(
            'namespace' => 'View_Helper',
            'path'      => 'views/helpers',
        ),
        'viewfilter' => array(
            'namespace' => 'View_Filter',
            'path'      => 'views/filters',
        ),
    ));
    $this->setDefaultResourceType('model');
}

All of the above resource types will be covered by default for every module that you follow this pattern for. Any others that you need will need to be customized in a bootstrap file.

ζ澈沫 2024-09-01 00:24:30

我使用 Zend Framework 版本 1.10.4 和默认项目配置。我使用以下行激活 application.ini 中的所有模块:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

模块目录中有一个 Bootstrap.php 很重要。如果没有它,我的模型就无法正确加载。您的浏览器中的 URL 似乎区分大小写。例如: http://example.com/Admin/controller/action

所以,它应该工作...

I am using Zend Framework Version 1.10.4 with a default project configuration. I activate all Modules in my application.ini with the following line:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

Its important that you have a Bootstrap.php in your module directory. Without that, my models wasn't loaded correctly. The URL in your Browser seems to be case sensitive. For example: http://example.com/Admin/controller/action

So, it should work...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文