开发者

Zend Autoloading models issue

Zend framework. I want to autoload my models classes inside models folder, from within bootstrap class. These models doesnt actually use any namespace (so I have Ex. User.php file's class named User and so on..).

If I understood correctly I should use the Zend_Loader_Autoloader_Resource and I tried:

function _initLoaderResource() 
{         
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(                 
    'basePath'  => APPLICATION_PATH,//points to the "application" path where resides "models" folder
    'namespace' =>''       
    ));         

    $resourceLoader->addResourceType('models', 'models/');

} 

And I receive following 'Zend_Loader_开发者_JAVA技巧Exception' message:

'Initial definition of a resource type must include a namespace' 

My questions are:

  • Is this the right way to autoload models?
  • How should I manage resource code that doesn't follow Zend Framework coding standard?


Actually you probably don't want to use the resource autoloader for this, since (as you've discovered) it requires a namespace. The standard autoloader (which loads models from the include path) has an option setFallbackAutoloader which tells ZF that that autoloader should be used for any class not matching a namespace covered by another. So all you need to do is ensure your models directory is on the include path and set this option to true.

You are probably already using the standard autoloader for loading the Zend classes, so you'll probably want to modify your application.ini file to add your model directory to the include path, and then set the fallback option either in application.ini or in your Bootstrap class:

protected function _initAutoloader()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    return $autoloader;
}


Zend Autoloader uses namespaces to make sure you are not using the autoload process, on those classes you don't want. So you would have to choose a namespace for your classes.

You could start your classes with an application specific namespace, or a general one. namespaces like 'My_' or 'App_' are general, yet for example if your application name is Job Board, you could use namespaces like 'JB_' in your class files.

You may also write your own autoloader (either a totally new one, or by extending the Zend autoloader) and register it as the SPL autoloader to bypass this.

Your class names does not have to follow the Zend Framework naming conventions, just make sure they have a namespace and register the namespace in the autoloader.

Here I attach a piece of my code that registers some resources to be autoloaded. I'm having multiple modules, and each module has a namespace regarding that module name. Please note that since there were many namespaces, I register them all in a loop.

    $nameSpaceToPath = array(
                            'Application'   => APPLICATION_PATH,
                            'Base'          => APPLICATION_PATH . '/base',
                            'Store'     => APPLICATION_PATH . '/modules/Store',
                            'Payment'     => APPLICATION_PATH . '/modules/Payment',
                            'Admin'     => APPLICATION_PATH . '/modules/Admin'
                        );

    foreach($nameSpaceToPath as $ns => $path) {
        $autoLoaderResource = new Zend_Loader_Autoloader_Resource(
                            array(
                                'basePath' => $path,
                                'namespace' => $ns
                            )
                        );
        $autoLoaderResource->addResourceType('controller','controllers','Controller');
        $autoLoaderResource->addResourceType('model','models','Model');
        $autoLoaderResource->addResourceType('mapper','models/mappers','Model_Mapper');
        $autoLoaderResource->addResourceType('service','services','Service');
        // I'm using _Util_ in the name of my utility classes, I place them in 'utils' directory
        $autoLoaderResource->addResourceType('util','utils','Util');
        $autoLoaderResource->addResourceType('plugin','plugins','Plugin');
        $autoLoaderResource->addResourceType('form','forms','Form');
        // I'm using _Exception_ in the name of my module specific exception classes, I place them in 'exceptions' directory
        $autoLoaderResource->addResourceType('exception','exceptions','Exception');
        $autoLoader->pushAutoloader($autoLoaderResource);
    }

When you are defining a resource type by calling:

$autoLoaderResource->addResourceType('service','services','Service');

You are actually telling Zend Autoloader that you have a type 'service' (1st param), which is placed in the directory named 'services' (2nd param), and you are using 'Service' token in the class names to specify classes of this type.

The above code tells Zend Autoloader to search for class Store_Service_Core in the path 'APPLICATION_PATH/modules/store/services/Core.php'.

As you can see I have registered the general 'Application' namespace for the APPLICATION_PATH path. This means that each class, starting with Application_ would be autoloaded from the APPLICATION_PATH. So forexample I have a class named Application_Init which uses some initialization tasks, and now Zend autoloads it from the path APPLICATION_PATH/Init.php.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜