9
votes

I am learning how to use Zend framework and realise that the action helper is something that would be useful. I have set up a default installation of Zend on my machine, but I dont know where the helper file needs to go, what I need to put in the bootstrap file and how I use it. Can anyone point me in the right direction please - the ZF user guide is not to clear to me.

Thanks John

4

4 Answers

16
votes

Two thoughts for where to place your custom action-helpers:

  1. In a separate, custom library
  2. In the folder application/controllers/helpers

These ideas are not exclusive. Functionality that is general enough to work in multiple projects should probably be pulled into a separate library. But for functionality that is application-specific, there is an argument that it could be somewhere in the application folder.

@Jurian has already described the "separate-library" approach. For app-specific helpers, you can do as follows:

For a helper called myHelper, create a class Application_Controller_Helper_MyHelper in the file application/controllers/helpers/MyHelper.php. In Bootstrap, you have something like:

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Application',
        'basePath'  => APPLICATION_PATH,
    ));

    Zend_Controller_Action_HelperBroker::addPath(
        APPLICATION_PATH . '/controllers/helpers', 
        'Application_Controller_Helper_');

    return $autoloader;
}

Then your helper can be invoked in a controller by using:

$this->_helper->myHelper;

As you can see, this presumes you are using appNamespace 'Application'. If not, you can (must!) modify your class names to accommodate your circumstance.

Cheers!

6
votes

You can place action helpers in your own library. Besides library/Zend where all the Zend stuff is around, you can create a library/MyLibrary folder (MyLibrary is arbitrary chosen) and put the action helpers there.

A good place is the library/MyLibrary/Controller/Action/Helper folder you need to create and place your action helper there (i.e. Navigation.php). In this file, create the class MyLibrary_Controller_Action_Helper_Navigation.

The next step is to add the action helper to the HelperBroker of the Zend Framework during bootstrap. Therefore, create a new method in your Bootstrap.php file and add this function:

protected function _initActionHelpers ()
{
    Zend_Controller_Action_HelperBroker::addHelper(
        new MyLibrary_Controller_Action_Helper_Navigation()
    );
}

One last remark is you need to configure the use of this library by adding this rule to your application.ini:

autoLoaderNameSpaces[] = "MyLibrary_"
1
votes

You can do this through your application.ini file like so

resources.view[] =
resources.view.helperPath.Default_View_Helper_ = APPLICATION_PATH "/views/helpers/"

Then in your views/helpers path you can create a file like Time.php. This file would contain the following code:

<?php

class Default_View_Helper_Time extends Zend_View_Helper_Abstract
{
    public function time()
    {
        $date = new Zend_Date();

        return $date->get(Zend_Date::TIME_MEDIUM);
    }
}

?>

To use this in your view scripts you would use

<?=$this->time()?>

Which would display the current time using your new View_Helper

0
votes

You can avoid having to register your action helper namespace and path within the Bootstrap.php by declaring them in the application.ini instead like so:

resources.frontController.actionHelperPaths.My_Controller_Action_Helper = APPLICATION_PATH "/controllers/helpers"

Simply replace My_Controller_Action_Helper with your desired namespace, and modify the helpers directory path accordingly.

The helper can be initialized the same way:

$this->_helper->myHelper;

As mentioned by the docs, registering the prefix or path of the helpers is usually preferred because helpers would not be initialized until they are called like in the snippet above.

Of course, instantiating and passing helpers to the broker is a bit time and resource intensive, so two methods exists to automate things slightly: addPrefix() and addPath().

http://framework.zend.com/manual/1.12/en/zend.loader.pluginloader.html

Adding the config entry to the application.ini follows the same suggested pattern.