How to make your own Zend Framework Resource Plugin

A recent release of Zend Framework intruduced Zend_Application: an organized way to bootstrap the framework without the need of an ugly boostrap.php file.
It comes with plugins support too, so let’s see how to create one.

With Zend_Application you can not only setup all the components required (Zend_Table, Zend_View, Zend_Navigation, ecc) but you can also setup your own custom plugin, called Resource.

In the Zend lingo a Resource is a plugin loaded by the Zend_Application and configured through the application.ini file or in the Bootstrap.php class.

A Resource can be used to setup a standard Zend component, routes, controller plugins or your custom component.

Lets see how.

Use the Bootstrap class

When you create an empty Zend Framework project with the zf create project command, you’ll find a Bootstrap.php file in your application folder.

In this file there is a Bootstrap class that you can use to define your own resources by simply creating a method with the _init prefix.

For example:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }
}

This setup a resource called Doctype.
This resource simply set the doctype property in the view (which is a resource too).

With the $this->bootstrap('view'); line, the Doctype resource requires the initialization of another resource: the View. In this way you can define dependencies in your resources.

You can even override the definition of a standard resource with your own, for example:

protected function _initView()    
{
     $view = new Zend_View();
     return $view;
}

In this way Zend_Application will use the view resource defined in your _initView method instead of the standard one.

With this method you can customize your Zend Framework with ease, but when you need to configure a standard component, like the db connection, there is a better approach: using the application.ini.

Use the application.ini file

Another gift of the zf create project command is the application/configs/application.ini file.

It contains all the configurations options of your application.
You can for example use it to configure your db:

[ini] resources.db.adapter = “pdo_mysql”
resources.db.params.host = “localhost”
resources.db.params.username = “root”
resources.db.params.password = “”
resources.db.params.dbname = “test”
resources.db.isDefaultTableAdapter = true
[/ini] You could use either the Bootrap or the application.ini method for this task, however I personally prefer this method when configuring parameters that changes across the servers (development, testing, production) like the include paths or the db connection options.

A better approach: Create a custom resource class

To improve reusability, you can create a custom resource class, that can be easily distributed and configured through your application.ini.

Its simple: in your library create a MyResource folder and a Custom class:

//In library/MyResource/Custom.php:
class MyResource_Custom extends Zend_Application_Resource_ResourceAbstract 
{
    public function init() {
        $this->getBootstrap()->bootstrap('view');
        $view = $this->getBootstrap()->getResource('view');
        $view->doctype('XHTML1_STRICT');
        return $view;
    }
}

In the init method, you can initialize your resource, and return it.
The object returned will go in the resources repository, and it will be accessible with the $bootstrap->getResource('custom') call in the other resources. (hint: is case insensitive)

To use your newly created resource, you can simply put that in your application.ini:
[ini] ;Add the libray/MyResource folder to the plugin search path:
pluginPaths.MyResource = “MyResource”
;enable your Custom resource:
resources.custom = true
[/ini] That’s all.

A resource can take configuration options too, for example:
[ini] ;Add the libray/MyResource folder to the plugin search path:
pluginPaths.MyResource = “MyResource”
;enable your Custom resource while setting the “doctype” option:
resources.custom.doctype = “XHTML1_STRICT”
[/ini] Zend_Application will automatically initialize a resource when you set a configuration option in it.
If our resource doesn’t expect a parameter, you can simply write resources.*resourcename* = true, like in the previous example.

You can access the options from within the resource through the $this->options variable. You can also define a setter method for the option:

//In library/MyResource/Custom.php:
class MyResource_Custom extends Zend_Application_Resource_ResourceAbstract 
{
    var $doctype = 'XHTML1_STRICT';
    //Setter method for the doctype option:
    public function setDoctype($value) {
        $this->doctype = $value;
    }

    public function init() {
        $this->getBootstrap()->bootstrap('view');
        $view = $this->getBootstrap()->getResource('view');
        $view->doctype($this->doctype);
        return $view;
    }

You are now ready to build your distributable plugin for Zend Framework, for other examples see the Zend/Application/Resource folder in the framework distribution or my MadaConsole resource.

1 thoughts on “How to make your own Zend Framework Resource Plugin

  1. Pingback: 網站製作學習誌 » [Web] 連結分享

Comments are closed.