Dimitri
Dimitri

Reputation: 959

ZF2 - How to get the url and the parameters in the view

I would like to know how I can make to get the url and the parameters in the view.

This code get the Controler name without the pamameters...

<?php echo $this->url();  ?>

Thanks you for your helping. Best regards,

Upvotes: 0

Views: 6992

Answers (3)

Choupi Novski
Choupi Novski

Reputation: 21

A very late response in case it could help someone.

You can get request params in the view using :

$routeMatch = $this->getHelperPluginManager()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch()

$params = $this->getHelperPluginManager()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch()->getParams();

Upvotes: 2

Volvox
Volvox

Reputation: 1649

You can't get them easily from view script. You can however get them in Controller and pass it to view.

For simple paramaters you can use: $this->view->params = $this->getAllParams()

If you need something more, you need to ask Request object for it. To get Request object: $this->view->request = $this->getRequest(). From now you can get all info from view by using eg. $this->request->getRequestUri()

Remember that you can use var_dump(get_class_methods($this->request)) to get list of all available methods.

Upvotes: 2

Harish Singh
Harish Singh

Reputation: 3329

Use following to get get url with parameter

$this->url('route', array('controller' => 'controller', 'action' => 'action', 'paramkey'=>'value'), array('force_canonical' => true))

Upvotes: 2

Related Questions