0

如何在模块路径的自定义目录中自动加载类。我的应用程序的结构如下

application
|_ modules
   |_admin
     |_api
     | |_Core.php
     |_elements
       |_Dialog.php

我有两个自定义目录,“api”和“元素”,当我实例化这两个类的对象时,我收到错误消息:“找不到致命错误类 Admin_Api_Core”。我尝试使用 registerNamespace 但它根本不起作用

Zend_Loader_Autoloader::getInstance()->registerNamespace('Admin_');
4

2 回答 2

3

看看 ZFs资源自动加载器。

将以下内容添加到您的 Bootstrap.php

protected function _resourceLoader()
{
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'      => 'APPLICATION_PATH',
        'namespace'     => '',
        'resourceTypes' => array(
            'acl' => array(
                'path'      => 'api/',
                'namespace' => 'Acl',
            ),
            'form' => array(
                'path'      => 'elements/',
                'namespace' => 'Element',
            ),
        ),
    ));
}

Api_Core 负载APPLICATION_PATH . '/api/Core.php
Element_Core 负载APPLICATION_PATH . '/elements/Core.php
Admin_Api_Core 负载APPLICATION_PATH . '/modules/admin/api/Core.php
Admin_Element_Core 负载APPLICATION_PATH . '/modules/admin/elements/Core.php

于 2011-06-07T13:16:33.843 回答
1

You can configure autoloading inside your Module_Bootstrap (almost same approach as in Benjamin Cremer's answer but module based). To do it - create file Bootstrap.php in /modules/admin folder with the following content:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{

    protected function _initAutoload()
    {
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'      => realpath(dirname(__FILE__)),
            'namespace'     => 'Admin',
            'resourceTypes' => array(
                'api' => array(
                    'path'      => 'api/',
                    'namespace' => 'Api'
                )
            )
        ));

        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Admin',
            'basePath'  => dirname(__FILE__),
            'resourceloader' => $resourceLoader
        ));
        return $autoloader;
    }

}

After that you'll be able to instantiate class Admin_Api_Core etc. (you should specify all resoursTypes). If you have many modules, you can create such bootstraps for all of them.

于 2011-06-08T09:28:18.210 回答