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.

Zend Framework: Creating your own view helpers

This short tutorial assumes that you are using Zend_Framework 1.8, the latest stable release as of this post. To learn more about View Helpers, the Zend Framework Manual as an entire page for it found here.

First, you need to create a helpers folder inside: applications/views. So the structure of your directories will look something like this:

--------/helpers (This is the directory where all your view helpers will be stored.)

Inside the helpers directory, create a file named BaseUrl.php. The file should contain something like this:

class Zend_View_Helper_BaseUrl {
    public function baseUrl() {
        $fc = Zend_Controller_Front::getInstance();
        return $fc->getBaseUrl();

To use your view helper, just do something like this in you view/layout files.

<!-- Assuming that this is a Layout / View file -->
The base URL for this application is: <?php echo $this->baseUrl() ?>
<!-- More HTML code here -->
This entry was posted in General and tagged , , , , , , . Bookmark the permalink.

8 Responses to Zend Framework: Creating your own view helpers

  1. Declan Butler says:

    What does your Bootstrap application.ini look like to get this working? I am trying to do this but running into an issue. What I have is in Bootstrap.php

    $view = $this->getResource(‘view’);
    $view->addHelperPath(APPLICATION_PATH . ‘/layouts/helpers’);

  2. Wenbert says:

    @Declan Butler, it just worked out of the box for me after using the new version of ZF. My Bootstrap.php looks like this: http://paste2.org/p/363802 And my application.ini has nothing special: http://paste2.org/p/363804

  3. Hey,

    The problem I was having was the class name. I forgot to prefix it with Zend_View_Helper_

    It was Matthew Weier O’Phinney (Zend guru) who kindly spotted the mistake.

  4. Jim Li says:

    Hi Wenbert,

    For generic view helpers, like baseUrl, most likely you’ll use them in pretty much all your ZF applications. It works better for me to actually put them out of the project application directory.

    I borrowed the idea from Rob Allen (author of Zend Framework in Action). My common view helper files end up in /library/MyApp/View/Helper.

    You just need to register the custom helper path to view. If you think this makes sense, I’ll make a post on my blog to get to the details.

  5. Wenbert says:

    Hi Jim Li,

    That makes sense. Would be great if you can share your post.


  6. Anderson says:

    I’m having a problem with my custom helpers. By some reason, i’m getting the following error when the helper is called for my view:
    Fatal error: Uncaught exception ‘Zend_Loader_PluginLoader_Exception’ with message ‘Plugin by name ‘SpecialPurpose’ was not found in the registry; used paths: Zend_View_Helper_Navigation_: Zend/View/Helper/Navigation/ My_View_Helper_: ./views/helpers/ Zend_View_Helper_: Zend/View/Helper/;./viewshelpers/’ in C:xamppincludez195ZendLoaderPluginLoader.php:406 Stack trace: #0 C:xamppincludez195ZendViewAbstract.php(1118): Zend_Loader_PluginLoader->load(‘SpecialPurpose’)

    Can you tell me why???

  7. Wenbert says:

    @Anderson, did you double check your class names for your helper?

    class Zend_View_Helper_SpecialPurpose {
        public function specialPurpos() {
  8. Lenin says:

    Thank you so much!! It just worked…

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>