An action helper is a class that plugs into the controller to provide services to actions. They expand a controller’s functionality without using inheritance and so can be reused across multiple controllers and projects. To add Zend custom controller helper we can add it in library.
Here I am creating a custom library for our purpose with the namespace ‘App’, so need to add it in application.ini to autoload these.
autoloadernamespaces[] = "App_"
For example I am creating an helper for action in App/Action/Helper so the prefix for the helper is App_Action_Helper. We need to initiate these action helper prefix by writing the following code in our bootstrap file.
protected function _initActionHelpers() { Zend_Controller_Action_HelperBroker::addPrefix("App_Action_Helper"); }
Now we can create a custom helper file of Common functions like as below.
class App_Action_Helper_CommonFunctions extends Zend_Controller_Action_Helper_Abstract { public function pr($data){ echo '<pre>'; print_r($data); echo '</pre>'; } }
App_Action_Helper_CommonFunctions is a child class of Zend_Controller_Action_Helper_Abstract that contain helper
functions to control the program flow.
For example we have created a common function pr to check what are the elements of varible $data. We can call this function in controller like as below:
$this->_helper->CommonFunctions->pr($data);
Some other helpers that are already in zend framework to help in controller actions:
To set a layout according to the action of controller
$this->_helper->layout()->setLayout('user');
To redirect within controller action
$this->_helper->redirector('action','controller','module');
Helper to disable layout and set no view to render
$this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender();
Hope this will help you!