Markel Mairs
Markel Mairs

Reputation: 741

How to generate URL using Module, controller and view?

Is there a way of generating a full url in zend if the Module, controller and view names are known?

Upvotes: 2

Views: 2175

Answers (1)

Tim Lytle
Tim Lytle

Reputation: 17624

I'm assuming you mean module, controller, and action, since the view is determined by the action (usually).

In the view:

echo $this->url(array('module' => $module, 
                      'controller' => $controller, 
                      'action' => $action));

Any parameters not set, default to the current values, so in any given view:

echo $this->url(); //link for the current request

The function also accepts two additional arguments: url($urlOptions, $name, $reset). $name allows you to specify a route name, and $reset will clear the generated URL of any current parameters.

In the controller:

This actually isn't documented, but follows the structure of the redirector helper (in fact, I believe it is used by the redirector helper):

$url = $this->getHelper('url')->simple($action, $controller, $module, $params);

You can also use the url() method, which follows the View helper:

$url = $this->getHelper('url')->url(array('module' => $module, 
                                          'controller' => $controller, 
                                          'action' => $action));

Upvotes: 3

Related Questions