开发者

Zend Framework: Autoloading a Class Library

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');
    开发者_JAVA百科    $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_');


you can define the autoload dir in the config.ini file like this:

autoloaderNamespaces[] = "Me_"


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

works 100%


I think @smack0007 means replace the contents of your _initAutoload method with Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_'); so it looks like this:

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


Not sure if this is your problem, but I just spent the last day and half trying to figure out my own similar problem (first time loading it up on Linux from Windows). Turns out I was blind to my library's folder name case.

/library
    /Tlib

is not the same as (on *nix)

/library
    /tlib

Class name is typically this

class Tlib_FooMe {
 ...
}

Hope this helps someone who is similarly absentminded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜