Version 1.8 of the Zend Framework introduced a whole new concept of bootstrapping an application. The core functionality is contained in theĀ Zend_Application component which supports a strictly object-oriented approach to bootstrapping. Although the new concept is fairly straightforward, it does take a little time to get used to it if you are used to the bootstrapping process in earlier versions of ZF.
One of the many interesting features of Zend_Application is the ability to push resources into resource plugin classes which can be utilized simply via configuration. In this tutorial we will be looking at creating a custom resource plugin. The plugin is quite simple, it creates a “value object” with the name, author and version of the application, and stores it in the Zend registry.
Standard Resource Plugins
Version 1.8 ships with a set of standard resource plugins that can be found in the library/Zend/Application/Resource folder:

It’s worthwhile to take a look at the source code of these classes. The use of the available resource plugins is described in the Reference Guide. The idea behind the resource plugins is to encapsulate common bootstrapping tasks like setting up a default database adapter, configuring the view, etc.
What makes the resource plugins etremely useful is the fact that the Zend Framework automatically initializes the plugins according to the options set in the application’s configuration file. You only have to make sure that you provide the proper combination of keys in the configuration file. The first key has to be “resources”, the second key has to be the name of the resource plugin, and any further keys should reflect the names of the required options. This way you can even add new resources without needing to touch the bootstrap class itself. (We will look at a sample configuration file shortly.)
The AppInfo Resource Plugin
The need to display the application name, version number or author name in different places of the UI (including the browser title) is quite common. There are several ways of doing this in ZF, but for the purpose of this tutorial we will be using a resource plugin which makes it easy to re-use this functionality between applications. Our sample Appinfo resource plugin works very similar to the Locale and Translate plugins. It collects information from the configuration file, instantiates an object and stores it in the Zend registry so that the information later can be retrieved from anywhere in the application.
Let’s take a look at the folder structure and the configuration file first:

Please note the different upper and lower case writing (Appinfo vs. AppInfo) which is due to ZF naming conventions.
In order to make this all work we need to provide some settings in the configuration file. Here’s an excerpt from the application.ini inside the application/configs folder (the project setup follows the default folder structure as it is created by Zend_Tool_Project).
-
# Plugin Paths
-
pluginpaths.MyApp_Resource = APPLICATION_PATH "/../library/MyApp/Resource"
-
-
# Resource Plugins
-
resources.appinfo.name = My Application
-
resources.appinfo.author = Stefan Schmalhaus
-
resources.appinfo.version = 1.0.0
-
-
# Autoloader Namespaces
-
autoloadernamespaces.0 = "MyApp"
As already criticized by Rob Allen, we have to use the key “pluginpaths” to define the directory where the plugin is stored, but use the key “resources” to configure it! (Please refer to the online documentation for more information on these two keys.) I also added the namespace “MyApp” to the Zend autoloader for all classes below the MyApp folder.
The AppInfo Helper Class
The class MyApp_AppInfo in the library/MyApp folder is a helper class which simply holds the information we want to make available via the Zend registry.
-
class MyApp_AppInfo
-
{
-
protected $_name;
-
protected $_author;
-
protected $_version;
-
-
public function __construct($name, $author, $version)
-
{
-
$this->setName($name);
-
$this->setAuthor($author);
-
$this->setVersion($version);
-
}
-
-
public function setName($name)
-
{
-
$this->_name = trim($name);
-
return $this;
-
}
-
-
public function getName()
-
{
-
return $this->_name;
-
}
-
-
public function setAuthor($author)
-
{
-
$this->_author = trim($author);
-
return $this;
-
}
-
-
public function getAuthor()
-
{
-
return $this->_author;
-
}
-
-
public function setVersion($version)
-
{
-
$this->_version = trim($version);
-
return $this;
-
}
-
-
public function getVersion()
-
{
-
return $this->_version;
-
}
-
}
The Plugin Class
The actual resource plugin class is MyApp_Resource_Appinfo, and like any other resource plugin class, it extends Zend_Application_Resource_ResourceAbstract. Here’s the complete code:
-
class MyApp_Resource_Appinfo extends Zend_Application_Resource_ResourceAbstract
-
{
-
const DEFAULT_REGISTRY_KEY = 'AppInfo';
-
protected $_appInfo;
-
-
public function init()
-
{
-
return $this->getAppInfo();
-
}
-
-
public function getAppInfo()
-
{
-
if (null === $this->_appInfo) {
-
$options = $this->getOptions();
-
-
$name = isset($options['name']) ? $options['name'] : '';
-
$author = isset($options['author']) ? $options['author'] : '';
-
$version = isset($options['version']) ? $options['version'] : '';
-
-
$this->_appInfo = new MyApp_AppInfo($name, $author, $version);
-
-
$key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
-
? $options['registry_key']
-
: self::DEFAULT_REGISTRY_KEY;
-
-
Zend_Registry::set($key, $this->_appInfo);
-
}
-
-
return $this->_appInfo;
-
}
-
}
When the resource plugin is initialized during bootstrapping, a new MyApp_AppInfo object is created if it doesn’t exist. This object gets populated with the data from the configuration file. (The inherited method getOptions() provides an array with the options that were defined in the application.ini file.) Finally, the object is stored in the Zend registry. All this is done automatically, we do not need to do anything “manually” in the bootstrap class or anywhere else.
Usage
Since the MyApp_AppInfo instance is stored in the Zend registry, we can retrieve the information from there. For example, in a controller we can assign the author name to a view variable like this:
-
class Some_IndexController extends Zend_Controller_Action
-
{
-
public function indexAction()
-
{
-
$this->view->author = Zend_Registry::get('AppInfo')->getAuthor();
-
}
-
}
Or if we wanted to display the application’s name inside the title tag of our web pages we could utilize the resource from within another resource plugin class. In this case a custom view resource plugin is the most appropriate place to do this:
-
class MyApp_Resource_View extends Zend_Application_Resource_ResourceAbstract
-
{
-
protected $_view;
-
-
public function init()
-
{
-
return $this->getView();
-
}
-
-
public function getView()
-
{
-
if (null === $this->_view) {
-
$options = $this->getOptions();
-
-
$this->getBootstrap()->bootstrap('appinfo');
-
$title = Zend_Registry::get('AppInfo')->getName();
-
-
$view = new Zend_View($options);
-
$view->doctype('XHTML1_STRICT');
-
$view->headTitle($title);
-
-
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
-
$viewRenderer->setView($view);
-
-
$this->_view = $view;
-
}
-
return $this->_view;
-
}
-
}
The important thing here is to enforce the initialization of the AppInfo resource:
$this->getBootstrap()->bootstrap('appinfo');
Finally, there is a possible pitfall that you should be aware of. Even if your plugins do not read any options from the configuration file, you still have to provide the key in the configuration file, otherwise the plugins won’t get initialized. For example:
-
# Resource Plugins
-
resources.appinfo =
-
resources.view =
If you want to learn more on resource plugins, I recommend to take a look at the source code of the standard plugins that ship with the Zend framework. I’m sure you will come up with your own ideas quickly.
It is not always the best idea to place your application resources in your own library. I make a very distinguished separation between library classes and application classes.
You AppInfo class is merely application specific and therefore should imho be stored in a resource folder inside your application folder. In the application.ini you could specify a resource folder, e.g. APPLICATION_PATH “/resources”.
It depends on the case to which part your class belong
Interesting intro for the resource system.
Thanks.
Excellent article, definitely helped me make better sense of the resource system. I have a few things I was hoping you could help clear up….
if you do not create an _initAppinfo function in the boostrap, where does the result of the Resource->init() go? I guess if you wish the result of the Resource plugin’s init function to be stored you would take the approach of making an _init function in the bootstrap?
Haha sorry, I see you are explicitly setting that using Zend Registry. I guess the online doc confused me a tiny bit with the storing of the result of the Bootstrap::_initWhatever function.