Tag Archives: zend framework

Adding a custom resource type to the Zend Framework autoloader

I’m working on a new project using the latest Zend Framework. I’ve got a modular application and decided that I wanted to hold my module-specific logic classes in their own ‘logic’ subdirectory.

Setting up class to folder mappings is done inside ‘Zend/Application/Module/Autoloader.php’ in the function ‘initDefaultResourceTypes’. In here you’ve got your standard mappings like ‘Form’ => ‘forms’, ‘Model’ => ‘models’ etc.

You’ve either got the choice of overriding this class with your own implementation, or you add your own resource types by putting the following code in your application or module bootstrap:

class MyApp_Bootstrap extends Zend_Application_Module_Bootstrap
{
 
    /**
     * Add some custom resource types to the resource loader
     *
     * @return void
     */
    protected function _initLoaderResource()
    {
        $this->getResourceLoader()->addResourceTypes(array(
            'logic' => array(
                'namespace' => 'Logic',
                'path'      => 'logic'
            )
        ));
    }
 
}

This will make the autoloader look inside the logic subdirectory for any classes with the namespace ‘MyApp_Logic’

Zend Framework Tool – project already exists here

When using the Zend Framework tool to create directory structures and some basic classes, you might come across the message “An error has occurred. A project already exists here”. A quick Google search found a couple of other people reporting this issue but no solution.

The problem occurs because by default the project provider searches all parent directories for a .zfproject.xml project configuration file. In my case I’d created a testing project further up the directory tree and neglected to delete it – this was fooling the tool into thinking a project already existed.

…which makes sense – you wouldn’t normally have a project inside a project (although you can’t rule out that somebody wants to do this)

Two solutions:

either look for a ZF project file in your parent directories:

find /path/to/htdocs -name '.zfproject.xml'

or, modify the code to search only the current directory if you have a directory structure that the tool doesn’t like

diff --git a/Zend/Tool/Project/Provider/Project.php b/Zend/Tool/Project/Provider/Project.php
index c24af5e..d3d75ea 100644
--- a/Zend/Tool/Project/Provider/Project.php
+++ b/Zend/Tool/Project/Provider/Project.php
@@ -61,7 +61,7 @@ class Zend_Tool_Project_Provider_Project
             $path = str_replace('\\', '/', realpath($path));
         }
 
-        $profile = $this->_loadProfile(self::NO_PROFILE_RETURN_FALSE, $path);
+        $profile = $this->_loadProfile(self::NO_PROFILE_RETURN_FALSE, $path, false);
 
         if ($profile !== false) {
             require_once 'Zend/Tool/Framework/Client/Exception.php';

ZF create project diff

The simplest way to apply this patch in Linux is to use the command:

patch Zend/Tool/Project/Provider/Project.php < createprojectfix.txt

Note: your mileage may vary – this was the cause of my particular issue, but it’s possible this happens for other reasons.