Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 - Generating Url from route

I can't figure out to generate Url from everywhere i want to, in zend 2

I get action and controller so i try this:

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

But this return an object, i just want the full URL string of this route

Somebody can help me to find the proper way?

EDIT : according to Stoyan, maybe i made a mistake on my route. here is the 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'));

the error : Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string

like image 647
Reign.85 Avatar asked Apr 02 '13 14:04

Reign.85


People also ask

How to generate a URL from Route?

Generating URLs from Route The main task of any route class is to determine whether this given route matches the HTTP request, and on match return the set of parameters by which a controller and action can be determined. An opposite task a route class allows to generate a URL by parameters.

How do I generate a URL from the urlcontroller plugin?

To generate a URL, you call the Urlcontroller plugin's fromRoute()method, as in the example below:

How do I generate a URL from an action method?

You can generate URLs inside your controller's action methods using the Urlcontroller plugin. To generate a URL, you call the Urlcontroller plugin's fromRoute()method, as in the example below:

How do I generate an absolute URL using urlview?

Generating Absolute URLs If you need to generate an absolute URL (having the scheme and host name), you can specify the third parameter for the Urlview helper. The third parameter should be an array containing one or several options. For assembling the absolute URL, pass the force_canonicaloption, as in the example below:


2 Answers

Use the echo before calling $this->url(...) (see bellow) and this will display the whole URL.

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

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

See this for more information about ZF2's URL view helper.

EDIT in response to the question edit:

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

If you want a URL in the controller then you need the URL controller plugin.

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

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

Hope this helps :)

Stoyan

like image 185
Stoyan Dimov Avatar answered Oct 21 '22 08:10

Stoyan Dimov


You can use this in the .phtml file

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

Where HelloWorld/default is the routing and the remaining is the controller and its action and also you can send the others parameter adding just in array as key and value pair.

like image 1
Azhar Ahmad Avatar answered Oct 21 '22 08:10

Azhar Ahmad