Equivalent of url() helper function in Zend controller

Mathew Attlee picture Mathew Attlee · Nov 5, 2009 · Viewed 29.5k times · Source

In the Zend view helper, there is the function url() for outputting a URL based on the routing tables eg

$this->url(array('controller' => 'comments', 'action' => 'add')

How can I do the same thing in a controller? In particular I want to set the action URL for a Zend Form using controller/action syntax rather than a standard URL eg

$form = new Zend_Form;
$form->setMethod('post')->setAction( $this->url(array('controller' => 'comments', 'action' => 'add')) );

Answer

Ferdinand Beyer picture Ferdinand Beyer · Nov 5, 2009

There is an action helper for this: Zend_Controller_Action_Helper_Url. Inside an action controller, you can access it using:

$this->_helper->url($action [, $controller [, $module [, $params]]]);

or:

$this->_helper->url->url(array(...));

Alternatively, you can also use the view helper:

$this->view->url(...);