I know there is already an accepted answer but I'll still post this to lessen the burden of beginners like me.
I use smarty as view renderer and in smarty you can't use php codes in the views, thus, everything must be done in the controller, allocating them in variables then passing them to the views.
You can use this:
$url = $this->url()->fromRoute('route',array('name'=>'route-name'));
If you are following the tutorial of zend 2, it will be 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'));
This will have a value of:
/zf2/index.php/album/add
/zf2/index.php/album/edit
/zf2/index.php/album/delete
As you can see, you need to add the server name to it, which you can do by using these before the generating the route url:
$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
Overall, 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://yourservername/zf2/index.php/album/add
Hope this helps beginner zf2 users