Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I drop the "Application_Model_" prefix from Zend Framework model class names?

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?)

like image 602
Peter Howe Avatar asked Aug 13 '10 11:08

Peter Howe


2 Answers

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
like image 178
Benjamin Cremer Avatar answered Sep 29 '22 08:09

Benjamin Cremer


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_"
like image 35
Andrei Serdeliuc ॐ Avatar answered Sep 29 '22 09:09

Andrei Serdeliuc ॐ