views:

62

answers:

2

I have a new ZF 1.10 project (just default module - not multiple modules) and am having to prefix my models' class names with "Application_Model_" in order for them to be picked up from the application/models directory.

How can I take more control of this? For example, I wish to namespace the model classes myself - e.g. as "Blah_ClassName" or perhaps even just "ClassName".

(I know I could use set_include_path() to achieve the latter but that's not very "Zend-like". I'm thinking some sort of change to the autoloader is needed - but what's the best way of doing it?)

A: 

For the autoloader to work, you need to do 2 things, add the path to your include_path and also specify an autoloader namespace:

Autoloadernamespaces[] = "YourNamespace_"
Andrei Serdeliuc
+2  A: 

You are looking for Resource Autoloaders.

In your bootstrap:

protected function _initResourceLoader()
{
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'namespace' => '',
        'basePath'  => APPLICATION_PATH,
    ));
    $resourceLoader->addResourceType('model', 'models/', 'Model');
    $resourceLoader->addResourceType('form', 'forms/', 'Form');
    $resourceLoader->addResourceType('service', 'services/', 'Service');

    return $resourceLoader;
}

To load resources:

$form    = new Form_Article    // loads from APPLICATION_PATH . /forms/Article.php
$model   = new Model_Article   // loads from APPLICATION_PATH . /models/Article.php
$service = new Service_Article // loads from APPLICATION_PATH . /services/Article.php
Benjamin Cremer