检索跨模块的应用程序资源列表的最简洁方法是什么?

时间:2011-02-25 10:32:41

标签: php zend-framework

首先,一些上下文

我目前正在使用Zend_Application开发模块化Zend Framework应用程序。我编写了一个自定义模块引导程序,它将自定义资源插入Module Resource Autoloader,例如“Widget”资源。

现在,假设以下结构:

/application
/application/modules/foo/widget/Bar.php
/application/modules/baz/widget/Qux.php

我如何能够在我的应用程序中检索每个可用小部件的列表,最好不要遍历我的整个目录结构?

1 个答案:

答案 0 :(得分:1)

不幸的是,我认为没有一个完美的解决方案。我认为最好的方法是在相应的模块引导中使用标准方式“注册”小部件,类似于模块特定的视图助手的工作方式。

创建一个用于管理在主应用程序引导程序中实例化的小部件的类:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initWidgets()
    {
        return new Yourapp_Widgets();
    }
}

然后在每个模块中:

class Foo_Boostrap extends Zend_Application_Module_Bootstrap
{
    protected function _initWidgets()
    {
        $widgetManager = $this->getApplication()->getResource('widgets');
        $widgetManager->registerWidget('Foo_Bar');
    }
}

然后,您可以在窗口小部件管理器类上拥有一个方法来返回所有已注册的窗口小部件。

相关问题