A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Creating and Registering your custom plugins in Zend Framework 1.8

When Zend Framework 1.8 was released, anyone could now setup a project within seconds using the zf Command Line Tool — see Quickstart Guide.

A few days ago, I managed to setup a project using the new command line tools. My project now had models, views and all the controllers that I needed. But then came the time when I had to setup my own custom plugins. Now, I had done this couple of times before. Prior to the 1.8 release, there was no command line tool and everyone had custom bootstrap files and their own configuration. The common thing to do was to do something like this: (maybe in the your bootstrap file)

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new FooPlugin());

Now, after then ZF1.8 release, ZF generated the bootstrap files for you, I thought it was probably a good idea not to edit the default files it created.

To register your own custom plugin in the new Zend Framework 1.8 release, go to your application.ini file found in the configs directory and add this:

autoloaderNamespaces[] = "My_"
resources.frontController.plugins.CheckHasAcess = "My_Controller_Plugin_CheckHasAccess"

This is assuming that you have your plugin in this directory:

/path/to/library/My/Controller/Plugin/CheckHasAccess.php

And the CheckHasAccess.php file contains something like this.

<?php
/**
 * Enter description here...
 *
 */
class My_Controller_Plugin_CheckHasAccess extends Zend_Controller_Plugin_Abstract {
 
    /**
     * Enter description here...
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        //code for your plugin here... :P
    }
}

Please note that Zend Framework has a naming convention on this. For the classname: My_Controller_Plugin_CheckHasAccess, every underscode (_) is converted into a slash (/) to form a directory structure. So, the classname ‘My_Controller_Plugin_CheckHasAccess’ would be read as /My/Controller/Plugin/CheckHasAccess.php by Zend Framework — with the ‘My’ folder inside the ‘library’ folder

So the key things to remember are:

  1. Register your custom plugin in your application.ini
  2. Make sure that you load your namespace in the application.ini. In this example, it should be ‘My’
  3. Make sure that you have your classname-to-directory structure is correct

FURTHER READING:
Zend Devzone has a very good article discussing about the custom Front Controller Plugin. It can be found here.

This entry was posted in General and tagged , , , , , . Bookmark the permalink.

15 Responses to Creating and Registering your custom plugins in Zend Framework 1.8

  1. Oliver says:

    Thanks for the tip.

    I’m using ZF 1.8 for the first time on a live project, and I must say that, while the framework is a pleasure to work with, the documentation is driving me mad. I can’t find any mention of the new resources object in the application.ini or its parameters, and, while I like that it can hook up a database automatically, I hate that I don’t know what’s going on under the hood for that to happen.

    How do I find out what options are available by default and which I have to instantiate manually? Can I get to instantiate – for example – an instance of Zend_Log?

  2. Wenbert says:

    @Oliver I’m glad you found the post useful :)

    Sometimes, I feel the same about the documentation. When I am confused I usually dig into the Zend Framework code — inside the /library/Zend folder — from there I take a peek at the files. I also do a lot of Zend_Debug::dump($var). If these get out of hand, I go to the Nabble mailing list: http://www.nabble.com/Zend-Framework-Community-f16154.html Matthew and the other Zend guys are always there to help.

    Searching Google using “site:zend.com keyword” is also useful.

  3. Pingback: Wenbert Del Rosario’s Blog: Registering your custom plugins in Zend Framework 1.8 | Webs Developer

  4. Zogs says:

    @Oliver
    Like Wenbert said, a lot of things can be found in the source code. Apart form a few really complex components (Zend_Form, Zend_Db) most of it isn’t really that hard to understand. As for resources, check the:
    library/Zend/Application/Resource
    folder. All the resources that are available out-of-the-box are in there. Unfortunately, this does not include Zend_Log.
    On the bright side, it’s quite easy to make your own resource plugin. Check: the manual on that. I did the same, also for Zend_Log ;)

    What i like to know is, if it’s possible to register plugins that take constructor arguments. And if so, how?
    The only way I can see now is to write your own frontcontroller resources, and register your plugins there the ‘old-fashion’ way. But that seems a bit dodgy..

  5. Mantu Tandurin says:

    Hi,

    and thanks for the article! You did great job. Actually I was working on something that should be done with ZF custom plugins and finally that lovely post. The only other author I found talking about Front_Controller plugins my be interesting to you. It’s here

  6. guelmismr says:

    Recently I started to get familiar with the MVC and I was recomended to work with Zend Framework, but it has taken me several days trying to configure the Boostrap.php, the .htacces and index.php on public folder, so far it works for the IndexController and ErrorController, but when adding new controllers is not very clear how should I call them, because I can not even define who is the front controller, I need a basic setup to begin to develop an application, the base. Could someone help me?

  7. Wenbert says:

    @guelmismr Have you looked at the Zend Framework Quickstart? I suggest that you read it first: http://framework.zend.com/docs/quickstart

    And also this: http://akrabat.com/zend-framework-tutorial/

    Both tutorials should be helpful.

  8. Pingback: Registering front controller plugins in Zend Framework 1.8 | King F1

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

  10. Giulio Prina Ricotti says:

    I wonder how this isn’t the first post I found searching for zend plugins around. All I was reading where 2007/2008 posts. This should article should be made accessible more easily to a greater audience

  11. Wenbert says:

    Thank you Giulio Prina. It would be great if you can add this post to your own blog or add it to your shareable bookmarks, etc.

  12. Excellent article, good looking blog, added it to my favorites.

  13. Mapics says:

    Muchas gracias por la ayuda voy a intentarlo y ver lo que sucede.

  14. coiby says:

    Thanks for this wonderful article!
    I’ve a question about registe a plugin in the application.ini, what does “CheckHasAcess” in “plugins.CheckHasAcess” mean? How would it make a difference if I change it to “plugins.has”
    ?resources.frontController.plugins.CheckHasAcess= “My_Controller_Plugin_CheckHasAccess”

    Thanks!

  15. Pingback: 关于Zend Framework 的自定义类库 — My December

Leave a Reply to Wenbert Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>