I know that there is already an accepted answer, but I will publish it anyway to reduce the burden of newcomers like me.
I use smarty as a rendering visualization tool, and in smarty you cannot use php codes in views, so everything should be done in the controller, highlighting them in variables, and then passing them to the views.
You can use this:
$url = $this->url()->fromRoute('route',array('name'=>'route-name'));
If you follow the zend 2 tutorial, it will look like this:
$url = $this->url()->fromRoute('album',array('action'=>'add')); $url = $this->url()->fromRoute('album',array('action'=>'edit')); $url = $this->url()->fromRoute('album',array('action'=>'delete'));
It will make a difference:
/zf2/index.php/album/add /zf2/index.php/album/edit /zf2/index.php/album/delete
As you can see, you need to add a server name to it, which you can do using them before generating the route URL:
$url = $uri = $this->getRequest()->getUri(); $url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
In general, the code snippet should look like this:
$url = $uri = $this->getRequest()->getUri(); $url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost()); $url .= $this->url()->fromRoute('album',array('action'=>'add'));
To produce:
http:
Hope this helps beginner zf2 users