Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
        $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.

like image 253
user250120 Avatar asked Jan 28 '10 01:01

user250120


1 Answers

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_');
like image 111
smack0007 Avatar answered Sep 22 '22 12:09

smack0007