1

I am going to write down a simple example to show what I need, because after 3 hours of research I didn't manage to find a similar thing to this.

I have all my classes starting with Project_. Eg. Project_BaseController is the base controller class.

The BaseController.php is located at /www/Core/ (that is one of the paths in set_include_path), but the autoloader searches it at /www/Core/Project/.

I would like to set the path to /www/Core/ for every class starting with Project_ (after that it should find the files after naming, like: Project_User => /www/Core/User.php)

Maybe Zend_Loader_Autoloader_Resource is what I need?

I doubt it because it is for resource's not kind of global config.

Littm
  • 4,923
  • 4
  • 30
  • 38
Adam Berecz
  • 989
  • 1
  • 6
  • 5

2 Answers2

1

The method you name your classes violates PSR-0 principal, which ZF has adopted. It would be easier for you to follow PSR-0. In this case the only thing you left to do is to relocate your Project_ classes to Project directory.

akond
  • 15,865
  • 4
  • 35
  • 55
0

The approach used in this answer may help. A custom auto loader seems to be the way to go here. –

Add the following line to application.ini

autoloadernamespaces[] = "App_"

Adjust this to match were you keep your library files, I keep mine in library/App.

Create your autoloader in App/Loader/Autoloader/Project.php like this:-

class App_Loader_Autoloader_Project implements Zend_Loader_Autoloader_Interface
{
    public function autoload($class)
    {
        $project = explode('_', $class);
        if ($project[0] != 'Project'){
            return false;
        }
        $project[0] = '/www/core';
        require_once implode('/', $project) . '.php'
        return $class;
    }
}

Initialise your autoloader in your bootstrap:-

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{
    protected function _initAutoloading() {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->pushAutoloader(new App_Loader_Autoloader_Project());
    }
}

Obviously, I don't have an environment to match yours, so I am unable to test this. However, I have adapted it from my own use of autoloaders, so it should work without too much tweaking.

I originally came across this technique thanks to an answer posted by David Weinraub and have found it extremely useful since.

Having said all that, akond's answer is good advice and would be the preferred option; work with the framework, not against it. I provide this answer in case that is not an option for you.

Community
  • 1
  • 1
vascowhite
  • 18,120
  • 9
  • 61
  • 77