9

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

Reign.85
  • 2,420
  • 1
  • 28
  • 28

2 Answers2

25

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

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
  • Thank you Stoyan for this fast answer, however, as i don't get a string, i can't echo it : Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string Maybe i made a mistake regarding my routes, i will edit my post to add my code – Reign.85 Apr 02 '13 at 14:41
  • @Reign.85 I edited my answer based on your edit. Hope this will give you idea of how to create the url :) – Stoyan Dimov Apr 02 '13 at 14:57
  • Tank you so much, this work for me, i just need to add the server url to the path – Reign.85 Apr 02 '13 at 15:05
1

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.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
Azhar Ahmad
  • 183
  • 12