Zend 框架:自动加载类库 [英] Zend Framework: Autoloading a Class Library

查看:24
本文介绍了Zend 框架:自动加载类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有以下引导程序:

'默认','basePath' =>目录名(__FILE__),));$autoloader->registerNamespace('Me_');返回 $autoloader;}/*** 引导视图文档类型** @return 无效*/受保护的函数 _initDoctype(){$this->bootstrap('view');$view = $this->getResource('view');$view->doctype('XHTML1_STRICT');}/*** Bootstrap 注册表和存储配置信息** @return 无效*/受保护的函数 _initRegistry(){$config = new Zend_Config_Ini(APPLICATION_PATH .'/configs/application.ini', APPLICATION_ENV,array('allowModifications'=>true));Zend_Registry::set('configuration', $config);}}

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

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

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

有什么想法吗?

可能相关的杂项:

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

有人建议更改默认",我尝试过,但它似乎没有解决问题,并且增加了使用此命名空间破坏模型功能的负面影响.

解决方案

你的班级需要命名为 Me_Myclass:

class Me_Myclass{}

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

<预><代码>//应用/图书馆/民众

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

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

I've got a class library in defined here .../projectname/library/Me/Myclass.php defined as follows:

<?php
class Me_Myclass{
}
?>

I've got the following bootstrap:

<?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);
    }

}

In my controller I try to instantiate the class like this:

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

When I navigate directly to http:/something.com/projectname/some?id=1 I get the following error:

Fatal error: Class 'Me_Myclass' not found in /home/myuser/work/projectname/application/controllers/SomeController.php on line x

Any ideas?

Potentially Pertinent Miscellany:

The autoloader seems to work when I'm extending models with classes I've defined in other folders under application/library.

Someone suggested changing the 'Default', which I attempted but it didn't appear to fix the problem and had the added negative impact of breaking function of models using this namespace.

解决方案

You class needs to be name Me_Myclass:

class Me_Myclass
{
}

Move your library folder up a level so that you have the folder structure:

/
    /application
    /library
    /public

And then in your Bootstrap add the following to the _initAutoload():

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

这篇关于Zend 框架:自动加载类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆