Zend Framework: Application resources

27 10 2009

In this second part of my Zend Framework quest, i will dive in the wonderfull world of application resource plugins. Zend_Application provide several standard resource plugins for database access, session management, routing etc. You can find more info about these standard Resource Plugins here.

The fun part of these Resource Plugins is that they can be configurated completely within the application.ini (or application.xml) configuration file and are loaded automatically by the Application Bootstrapper.  For example you can define a new Route resource “myroute” referencing the module/controller/action without writing any code like this:

; Router resources
resources.router.routes.myroute.route = "hello/:myname"
resources.router.routes.myroute.defaults.module = "mymodule"
resources.router.routes.myroute.defaults.controller = "index"
resources.router.routes.myroute.defaults.action = "index"

Which will route the following url “http://localhost/hello/world” to the standard controller action from the “mymodule” module, with the request parameter “myname”  filled with “world”.

While Zend Framework ships with a number of standard resource plugins, the intention is that developers should write their own to encapsulate their own initialization needs. So lets begin with our own custom resource plugin for initializing application logging using Zend_Log.

The first step is to define a plugin-path and prefix path for the new custom resource in the application.ini configuration file (preferably somewhere in the beginning), like this:

pluginPaths.Custom_Resource = APPLICATION_PATH "/resources"

When the new resource class starts with “Custom_Resource”, it will be automatically loaded by the Bootstrapper.

Create a new file Logger.php in the APPLICATION_PATH/resources location containing the following code:

class Custom_Resource_Logger
extends Zend_Application_Resource_ResourceAbstract {

The new resource plugin should now accept options to the constructor, have mechanisms for setting and retrieving options, have mechanisms for setting and retrieving the bootstrap object, and an initialization method.

Now the basics are set, it is time to think about what the new logger resource can do, or better, define the configuration options. Here are the options:

  1. “registry”
  2. “output”
  3. “priority”
  4. “file”

Although Resource Plugins are put in a bootstrap container (which is basically an instance of Zend_Registry) they are not globally available. This means you need access to the bootstrap in your code to load the resource. To overcome this “problem” the Zend_Log instance is registered with the name provided in the “registry” option. This way you can get your Zend_Log instance like this:


The “output” option let you configure where the log messages are send: The available values are “file” or “firebug”.

The “priority” option will filter the log messages to the default Zend_Log priorities levels 0-7.

The “file” option finally defines the file location when you choose “file” as the output. It is possible to use a date within the filename, using the replace needle “%d”.

The full implementation (source) of the Logger.php file can be found here.

Just the final step to completion, registration off the new Resource Plugin to your bootstrap. Again this can be accomplished by adding the following line to your application.ini configuration file:

resources.logger[] = ""

The application.ini configuration should now look like this:

; Custom resource plugins
pluginPaths.Custom_Resource = APPLICATION_PATH "/resources"
; Logger resources
resources.logger[] = ""
; Option used for Global Registry location
resources.logger.registry = "Zend_Log"
; Use for output option: firebug or file
resources.logger.output = "file"
; Priorities available: 0=[EMERG]/1=ALERT/2=CRIT/3=ERR/4=WARN/5=NOTICE/6=INFO/7=[DEBUG]
resources.logger.priority = 3
resources.logger.file = APPLICATION_PATH "/../tmp/app-prod-%d.log"



One response

3 11 2009
Zend Framework Blog » Blog Archive » Aus den Zend Framework Blogs

[…] Bei Gerton findet sich ein kleiner Beitrag über Application Resources. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: