Zend Controller Action Helper

Zend Controller Action Helpers allow you to abstract common functionality into classes that are callable from any controller action. I won't go into detail as to why you'd want to use this, except to say that it's sometimes more flexible than extending your own abstract controllers.

Looking through the Zend Framework documentation and some online tutorials, there seems to be a little confusion on exactly how to initialize these. This post aims to clear up any confusions and help get you started as quickly as possible.

Step 1: Create a directory somewhere in your application folder, for example /application/helpers.

Step 2: Create a file called HelloWorld.php and save it in the helpers directory, i.e. /application/helpers/HelloWorld.php

Step 3: Write the source code of the HelloWorld file as below. Pay special attention to the class name:
<?php
class MyHelpers_HelloWorld extends Zend_Controller_Action_Helper_Abstract {
public function sayHello($name) {
return 'Hello' . $name;
}
}
?>

Note: The classname is MyHelpers_HelloWorld but the file name is just HelloWorld.php

Step 4: Place the following code in your bootstrap file or initializer:


Zend_Controller_Action_HelperBroker::addPath ('/application/helpers', 'MyHelpers');
Note: The second parameter is the prefix we added to the actual class name without the underscore. This prefix should not appear in the filename!

Step 5: Our new helper should now be ready for use inside any controller action. It may be called as follows:
$this->_helpers->HelloWorld->sayHello('Bob');

Note: There's a few different ways to call the helpers, including the direct() method which may be a handy shortcut. Refer to Zend documentation for these.

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).

Comments

  1. same content modified from other site........that too bad.

    ReplyDelete
  2. I went through a few different tutorials when I was trying to learn this but found that most skipped over some details or had typos or just weren't very clear. After spending too much time trying to work out exactly how to do it I wrote this step-by-step tutorial which I hope to be complete and save others some headache.

    ReplyDelete
  3. Hi Richard,

    Great to have this simply explained in one place. I am having problems though. I am pretty sure I followed the instructions exactly (even the same names and code to be sure) and it can't find the Helper.

    Any ideas?
    All the best,
    Ash

    ReplyDelete
  4. Ha, as is typical when asking for help, it now works. I had the Zend_Controller_Action_HelperBroker::addPath too low in the bootstrap.php. I stuck it up the top and it now works :)

    ReplyDelete
  5. Finally some decent explanation on action helpers! Thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word