jump to navigation

Zend Framework : A basic example of Zend Controller Action Helper April 24, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

As all my Zend Framework articles , this is a reminder for me , and hope it helps some newcomers to the Zend Framework . Zend Controller Action Helpers allow you to abstract common functionality into classes that are callable from any controller action . The official website makes a better definition for this topic :

Action Helpers allow developers to inject runtime and/or on-demand functionality into any Action Controllers that extend Zend_Controller_Action. Action Helpers aim to minimize the necessity to extend the abstract Action Controller in order to inject common Action Controller functionality.

Action Helpers are  for reusable but optional segments that your controller might need to access  , so you don’t have to write  (copy-paste ) common functionality over and over again on each Action-Controller that may need that specific functionality . The Zend Framework provides many build in Action Helpers (like redirector , FlashMessenger , ViewRenderer …..  ) . This article is how to create and access our custom Action Helpers , so let’s start .

  1. Creating a directory somewhere in your application folder, for example
  2. Add in the public/index.php  file the following configurations
    require_once ‘Zend/Controller/Action/HelperBroker.php’ ;
    Zend_Controller_Action_HelperBroker::addPrefix(‘helpers’) ;==========  OR Alternatively =============

    Add the following line inside the application.ini  file :

    resources.frontController.actionhelperpaths.Myhelpers = APPLICATION_PATH “/../library/helpers”
    Note : ” Myhelpers ” represents the prefix of our Class-name  , while  “helpers” represent the directory – name in the library directory wherein our Class is hosted .

  3. Writting the Class inside the Pre defined Action Helper Directory (library/helpers) : The Class must exted   “Zend_Controller_Action_Helper_Abstract ”  , Pay special attention to the class name it must be prefixed  ( in our example : “helpers” )
    class helpers_Myfirsthelper extends Zend_Controller_Action_Helper_Abstract {
    public function returnmsg($value) {
     return 'Hello your message is : ' . $value ;
  4. Our new helper should now be ready for use inside any controller action. It may be called as follows :
    echo $this->_helper->Myfirsthelper->returnmsg( $val)  ;

My example is of course an over- simplified demonstration , for a real world  scenario read this article written by W.Gilmore

And that’s it! If you followed the prefix convention and your path is correct, than your helpers should now work . It’s a little strange how Zend requires a different classname from the filename for helpers, which I think is where most people get caught out when starting out (I know I did).

Read this nice article from Zend Developers Zone


1. Brian - April 4, 2012

You, sir, deserve a cookie. I could not make heads or tails of the official Zend docs on Action Helpers, or even if they were what I really needed. Your quick write-up was exactly what I needed to get going.

2. vinojcardoza - August 21, 2013


Many Thanks for the nice tutorial.

Just to mention that the class name should be

class Myhelpers_Myfirsthelper

instead of

class helpers_Myfirsthelper


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