ZF2 - Generating URLs from a Route

I can't figure out how to generate Url everywhere I want in zend 2

I get the action and controller, so I try this:

$this->url('myControllerName', array('action' => 'myActionName')); 

But this returns an object, I just want the full URL string of this route

Can anyone help me find the right way?

EDIT: According to Stoyan, maybe I was wrong on my route. here is part of my module.config

 'router' => array ( 'routes' => array ( 'indexqvm' => array ( 'type' => 'segment', 'options' => array ( 'route' => '/Indexqvm[/:action][/:id_event]', 'constraints' => array ( 'action' => '[a-zA-Z][a-zA-Z0-9_-]+', 'id_event' => '[0-9]+' ), 'defaults' => array ( 'controller' => 'Qvm\Controller\Indexqvm', 'action' => 'index' ) ) ), 

And my call:

 echo $this->url('indexqvm', array('action' => 'list-index')); 

error: Catchable fatal error: Object of class Zend \ Mvc \ Controller \ Plugin \ Url cannot be converted to a string

+8
url php zend-framework2 action
source share
2 answers

Use the echo before calling $this->url(...) (see below) and it will display the entire URL.

 <?php echo $this->url('route-name', $urlParams, $urlOptions); ?> 

Note that the first url() parameter is the route name specified in your [module]/config/module.config.php .

See this one for more information on the ZF2 URL Browsing Assistant.

EDIT in response to the question:

The above section is related to using the URL browsing helper.

If you need a URL in a controller, you need a URL controller plugin.

 <?php $url = $this->url()->fromRoute('route-name', $params, $options); ?> 

This is a reference to the ZF2 manual for this controller plugin.

Hope this helps :)

Stoyan

+22
source share

You can use this in a .phtml file

 echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration')); 

Where HelloWorld / default is the routing, and the rest is the controller and its action, and you can also send the rest of the parameters, adding only an array as a pair of keys and values.

+1
source share

All Articles