如何执行模块引导资源存在于另一个模块's引导在Zend框架1


How to execute module bootstrap resources present inside another module's bootstrap in Zend Framework 1?

我在各个模块目录中引导Zend_Application_Module_Bootstrap的应用程序。我怎么能要求资源内的另一个模块的引导首先执行?

// app/modules/user/Bootstrap.php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initUser()
    {
    }
}
// app/modules/author/Bootstrap.php
class Author_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initAuthor()
    {
        $this->bootstrap('user'); // Fatal:  Resource matching 'user' not found
    }
}

我决定使用插件来实现这种细粒度的功能,因为执行顺序不能正确管理,因此使模块引导成为放置依赖代码的糟糕选择。

参考下面的答案来做决定:

按一定顺序从每个模块加载/执行基于模块的引导

根据来自ZF1邮件列表的这个线程,您可以通过应用程序引导的模块资源访问模块引导。

天啊,真拗口。我的意思是:

// app/modules/user/Bootstrap.php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initUser()
    {
    }
}
// app/modules/author/Bootstrap.php
class Author_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initAuthor()
    {
        $app = $this->getApplication(); // it's actually the application *bootstrap*
        $app->bootstrap('modules');
        $modulesResource = $app->getResource('modules'); 
        $userBootstrap = $modulesResource->user;
        $userBootstrap->bootstrap('user'); // should be cool
    }
}

根据我自己的经验,只要我的一个模块级资源需要在多个模块中引用——特别是在引导过程中——我就把该资源的引导推到应用级引导中。