Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Action helper

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

like image 726
user505988 Avatar asked Jan 15 '11 17:01

user505988


2 Answers

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!

like image 176
David Weinraub Avatar answered Dec 09 '22 14:12

David Weinraub


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_"
like image 32
Jurian Sluiman Avatar answered Dec 09 '22 12:12

Jurian Sluiman