Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of url() helper function in Zend controller

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')) );
like image 715
Mathew Attlee Avatar asked Nov 05 '09 14:11

Mathew Attlee


4 Answers

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(...);
like image 148
Ferdinand Beyer Avatar answered Oct 20 '22 20:10

Ferdinand Beyer


I've actually found out that only this works:

// in your form
public function init()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $url = $router->assemble(
        array(
            'paramterName0' => 'parameterValue0',
            'paramterName1' => 'parameterValue1',
        ),
        'routeName'
    );

    $this->setAction($url);
    ...
}
like image 21
Attilio Avatar answered Oct 20 '22 19:10

Attilio


Was able to answer my own question as it seems the following code does the trick:-

$form = new Zend_Form;
$form->setMethod('post')->setAction( $this->getHelper('url')->url(array('controller' => 'index', 'action' => 'add')) );
like image 2
Mathew Attlee Avatar answered Oct 20 '22 20:10

Mathew Attlee


In zf3 you can use:

    $form = new YourFormClass();
    $form->setMethod('post')->setAction($this->url()->fromRoute(array('controller' => 'index', 'action' => 'add'));
like image 1
Andrii Krot Avatar answered Oct 20 '22 18:10

Andrii Krot