ZEND: Load custom Class in Module Bootstrap

306 Views Asked by At

I want to call my custom class that is located in myModule/myFolders/myFile.php in myModule/Bootstrap.php

This is how it looks right now:

    <?php

    class Users_Bootstrap extends Zend_Application_Module_Bootstrap {

        public function _initAutoload() {
        Zend_Loader_Autoloader::getInstance()->pushAutoloader(new NAMESPACE_Excel_ExcelAutoLoader()); // This workes fine

        new Myfolder_Myfile();  // THIS IS WHERE I'm CALLING my CLASS

}

        public function _initRoutes() {

            $router = Zend_Controller_Front::getInstance()->getRouter();

             $listUsersRoute = new Zend_Controller_Router_Route("admin/users/manage", array(
               'module' => 'users',
               'controller' => 'admin',
               'action' => 'index'      ));

            $router->addRoute('listUsersRoute', $listUsersRoute);


        }     }

if I move my custom class to myModule/Modles folder and call it from there it works.

I understand that Zend Bootstrap knows and automatically loads locations as Views, Controllers and Models from my Modules. So how can I make it load myFolder too?

1

There are 1 best solutions below

0
On

There are many ways to accomplish what you are trying to do. First, I would ask - is it required that the MyFolder_MyFile() class be reused in many places? If not, you might consider converting that class' logic into its own custom bootstrap - which extends your Users_Bootstrap. Then, you'd have something like this:

MyFolder_MyFileBootstrap extends Users_Bootstrap

Then, you could choose to create individual _init* methods - or to just stay as compatible as you wrote it, you might just make a new _initAutoload() that executes the parent first - and then any of your own logic...

MyFolder_MyFileBootstrap::_initAutoload()

public function _initAutload() {
    parent::_initAutload();
    // my own logic here
}

However, if this solution would drastically change the way you're coding your application, you will need to add the file in using the namespace methods of the Zend Framework Autoloader.

For your example, you may try this inside of your _initAutoloader() method:

$applicationAutoloader = $this->getResourceLoader();
$applicationAutloader->addResourceType('myfolder', 'MyFolder', 'MyFolder');

Depending on your layout of the files, you might have to tweak this a bit.