Pragmatism in the real world

Custom Zend_Application Resources

Sooner or later, you want to leverage Zend_Application better by creating your own resource plugins. This lets you reuse your initialisation work in multiple application that much easier and keeps your Boostrap class that much shorter!

In my case, I wanted to create a resource for CouchDb that checked that the database was created and if not, create it.

Creating your own plugin is easy enough. The obvious place is library/App/Application/Resource and a typical resource would look like this:

class App_Application_Resource_Couchdb extends Zend_Application_Resource_ResourceAbstract
     * Defined by Zend_Application_Resource_Resource
     * @return Phly_Couch|null
    public function init()
     	// do stuff here to init CouchDB
        $options = $this->getOptions(); 
        // $options contains everything under 'resources.couchdb' in application.ini

You then need to tell Zend_Application about your new plugins. This is done with this line in application.ini:

pluginPaths.App_Application_Resource_ = "App/Application/Resource"

You can now have as many resource plugins as you like within the App_Application_Resource_ class-space.

Also, Matthew Weier O’Phinney has also written an article on Zend_Application which you should read too.

2 thoughts on “Custom Zend_Application Resources

  1. Fernando: Generally anything you do that is either a global resource (like CouchDB connections) or a plugin required at boot is usually tossed in its own resource.

    I usually keep around separate resources for my Cache object, Doctrine setup and connection, ACL setup, Debugging plugins, jQuery setup, etc.

    So yes, it would most likely be best to make your Smarty integration a separate application resource.

Comments are closed.