【问题标题】:Zend Framework: Autoloading a Class LibraryZend 框架:自动加载类库
【发布时间】:2010-01-28 01:51:15
【问题描述】:

我在这里定义了一个类库 .../projectname/library/Me/Myclass.php 定义如下:

<?php
class Me_Myclass{
}
?>

我有以下引导程序:

<?php

/**
 * Application bootstrap
 * 
 * @uses    Zend_Application_Bootstrap_Bootstrap
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Bootstrap autoloader for application resources
     * 
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Default',
            'basePath'  => dirname(__FILE__),
        ));
        $autoloader->registerNamespace('Me_');
        return $autoloader;
    }

    /**
     * Bootstrap the view doctype
     * 
     * @return void
     */
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    /**
     * Bootstrap registry and store configuration information
     * 
     * @return void
     */
    protected function _initRegistry()
    {
      $config = new Zend_Config_Ini(APPLICATION_PATH . 
                                      '/configs/application.ini', APPLICATION_ENV,
                                      array('allowModifications'=>true));
      Zend_Registry::set('configuration', $config);
    }

}

在我的控制器中,我尝试像这样实例化类:

<?php
class SomeController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $classMaker=new Me_Myclass();
    }
}
?>

当我直接导航到 http:/something.com/projectname/some?id=1 时,我收到以下错误:

致命错误:在第 x 行的 /home/myuser/work/projectname/application/controllers/SomeController.php 中找不到类“Me_Myclass”

有什么想法吗?

可能相关的杂项:

当我使用我在 application/library 下的其他文件夹中定义的类扩展模型时,自动加载器似乎可以工作。

有人建议更改“默认”,我尝试过,但似乎并没有解决问题,并且使用此命名空间破坏模型的功能产生了额外的负面影响。

【问题讨论】:

    标签: php zend-framework autoload zend-autoloader


    【解决方案1】:

    你的班级需要命名为 Me_Myclass:

    class Me_Myclass
    {
    }
    

    将您的库文件夹上移一级,以便您拥有文件夹结构:

    /
        /application
        /library
        /public
    

    然后在您的 Bootstrap 中将以下内容添加到 _initAutoload():

        Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
    

    【讨论】:

    • 您好,感谢您的回答,我尝试了这个并收到以下错误。这说明什么?致命错误:/home/myuser/work/myproject/library/Zend/Loader/Autoloader/Resource.php:128 中未捕获的异常 'Zend_Loader_Exception' 和消息 'Method 'registerNamespace' is not supported' 堆栈跟踪:pastie.org/799074跨度>
    • 请更新您的答案以显示您对代码所做的更改。
    【解决方案2】:

    您可以像这样在 config.ini 文件中定义自动加载目录:

    autoloaderNamespaces[] = "Me_"
    
    
    ;You could add as many as you want Classes dir:
    autoloaderNamespaces[] = "Another_"
    autoloaderNamespaces[] = "Third_"
    

    100% 有效

    【讨论】:

      【解决方案3】:

      我认为@smack0007 意味着用 Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_'); 替换你的 _initAutoload 方法的内容;所以它看起来像这样:

      protected function _initAutoload()
      {
          Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
      }
      

      【讨论】:

        【解决方案4】:

        不确定这是否是您的问题,但我刚刚花了最后一天半的时间试图找出我自己的类似问题(第一次从 Windows 在 Linux 上加载它)。原来我对图书馆的文件夹名称大小写视而不见。

        /library
            /Tlib
        

        与(在 *nix 上)不同

        /library
            /tlib
        

        类名通常是这样的

        class Tlib_FooMe {
         ...
        }
        

        希望这对同样心不在焉的人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 2011-01-07
          • 2011-12-07
          • 2011-05-25
          • 1970-01-01
          • 2012-03-10
          相关资源
          最近更新 更多