jurian sluiman

{
Switch to
De taal Nederlands is niet beschikbaar. De taal Engels wordt getoond.

Eenvoudige toegang tot action helpers

It's -for me- a pain in the ass to write too much code when it isn't necessary. For example, you're able to fetch a view helper simply by using the magic method Zend_View_Helper_Abstract::__call(), but when you try to magically load an action helper in an action controller, this is (by default) not possible.

This article is just a small talk how to call the direct() method of the action helper and get the instance of the helper.

First, we need to override the __call() of Zend_Controller_Action and add a __get() to it. Therefore I created an extended class in my own library:

class My_Controller_Action extends Zend_Controller_Action {}

And in this class we'll override the __call():

public function __call($methodName, $args)
{
    require_once 'Zend/Controller/Action/Exception.php';
    if ('Action' == substr($methodName, -6)) {
        $action = substr($methodName, 0, strlen($methodName) - 6);
        throw new Zend_Controller_Action_Exception(sprintf('Action "%s" does not exist and was not trapped in __call()', $action), 404);
    } elseif (null !== ($helper = $this->_helper->getHelper($methodName))) {
        // Added to have direct access to helper objects
        return call_user_func_array(array($helper, 'direct'), $args);
    }
    throw new Zend_Controller_Action_Exception(sprintf('Method "%s" does not exist and was not trapped in __call()', $methodName), 500);
}

And add a __get():

public function __get($property)
{
    return $this->_helper->getHelper($property);
}

Now it's really easy to access action helpers inside your controller action:

public function indexAction ()
{
    // Proxies to Zend_Controller_Action_Helper_Url::direct()
    $url = $this->url('myAction', 'myController', 'myModule');

// Proxies to Zend_Controller_Action_Helper_Url::simple()
$url = $this->url->simple('myAction', 'myController', 'myModule');
}

Enjoy!

Reacties

Er is nog geen reactie geplaatst. Wees de eerste met het plaatsen van een reactie!

Plaats een reactie

Als je een gebruiksaccount hebt voor deze site, kan je hier klikken om in te loggen.

Merk op dat een al ingevoerd bericht na het inloggen verwijderd zal zijn!

 
 

Het adres wordt intern opgeslagen maar niet weergegeven op deze site. Ik vraag er alleen om als extra controle.

In het bericht is geen html toegestaan. Een witregel vormt een nieuwe paragraaf en urls krijgen een hyperlink.