Simple Controller Plugin in Zend Framework

Simple Controller Plugin in Zend Framework

This tutorial will describe how to create own Controller Plugin.
How to set up environment for zend framework controller plugin.
How extend the library with own class and finally how to use it.

Plugin is nothing else but the class, in our case it extends Zend_Controller_Plugin_Abstract.
First of all you need name for your plugin, why?
Because we need to set up enviroment for plugin usage and we start with the name.

I called my plugin “Example_Controller_Plugin_Param”, it means that I have created file (class) called “Param.php”
in “library/ Example/Controller/Plugin/”.
Then in “application/Bootstrap.php” class I created method “_initAutoload” in which we need to register nampspace.

< ?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initAutoload() { /* * If you don't register namespace * You will get error : * Fatal error: Class 'Example_Controller_Plugin_Param' not found in * ...\library\Zend\Application\Resource\Frontcontroller.php on line 92 * */ $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace('Example_');
$autoloader->suppressNotFoundWarnings(true);
 
/*
* also you will get Exception :
* No entry is registered for key 'Zend_Request_Example'
* called in helper ParamHelper.php
*
*/
 
}
 
}

Also in “application/configs/application.ini” file we need to prepare plugin ready for usage with line:
resources.frontController.plugins.param = “Example_Controller_Plugin_Param”

When you do this steps, check your application if you got some errors or warnings before you go to coding.
Now when we set up environment, we can start with coding our plugin.

I tried to simplyfie this example so our plugin will only create new custom request parameter,
which will appear when you debug

Zend_Debug::dump($this->_request->getParams()); 

in any Controller class.
And it will start the session, create session namespaces with simple logic which will check if the session params are set or not and it will throw an Exception.

< ?php class Example_Controller_Plugin_Param extends Zend_Controller_Plugin_Abstract { protected $_param; private $_userID; public function routeShutdown(Zend_Controller_Request_Abstract $request) { /* * we start session here * so we do not need to start it in each action, controller or module */ Zend_Session::start(); /* * we create session with namespace 'login' */ $namespace = new Zend_Session_Namespace('login'); /* * */ $this->_userID = $namespace->user;
 
/*
* create some simple logic, just to see application behaviour
*/
 
if (!isset($this->_userID) or $this->_request->getParam("user") != null) {
$this->_userID = $this->_request->getParam("user", null);
 
if ($this->_userID == null) {
throw new Exception("user id not found");
}
 
$namespace->user = $this->_userID;
}
 
/*
* Just for example we also create new custom request parameter
* with name and value, cannot set this in helper
*
*/
$this->_param = $this->_request->setParam('param', 'custom');
 
Zend_Registry::set('Zend_Request_Example', $this->_param->param);
 
}
 
}


What is so special here?
Well, in this example you do not need to create session namespaces in any action, controller or module and this is something what spares our development time and lines of code.

Source code link: zf.plugin.example
Test your new plugin and enjoy in coding with Zend Framework.

You made it all the way down here so you must have enjoyed this post! You may also like:

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

Consuming Magento REST service using Zend_OAuth_Consumer Darko Goles
Darko Goles, | 45

Consuming Magento REST service using Zend_OAuth_Consumer

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php Darko Goles
Darko Goles, | 1

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php

3 comments

  1. Thanks for writing it down in such an understandable way. I had trouble finding all the pieces of the puzzle on one page.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.