Tuesday, 19 February 2013

Access action helper from another helper in Zend Framework 1

In Zend Framework 1, i have seen lot of people accessing action helper inside another helper by following these two approaches

Approach 1:

a) Create an instance of the controller helper class
b) call the method of the class using object

<?php

$helperObj = new Controller_Action_Helper_Test()
$helperObj->direct();

?>

Approach 2:

<?php
//controller helper class name
$helperObj = $this->getActionController()->getHelper('Test');  
$helperObj->direct();
?>
I am not here to argue the above two approaches are not correct. Both will work as expected
.I am saying this is not the right way to access it.  Below is the right way to access action helper inside another helper.

<?php
$helperObj = Zend_Controller_Action_HelperBroker::getStaticHelper('Test')->direct();
?>
Hope, you enjoyed this post. Did you have any other better approach?

No comments:

Post a Comment