Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2: Zend Framework 2 Full URL including host name

In my view I need to draw full URL. Like this:

http://hostename.com/default/url

When I try to use $this->url('default', array(1,2,3)) I get only /index/get/. Is there any Zend method to het host name or I have to use $_SERVER['HTTP_HOST'] instead?

like image 902
Sergey Romanov Avatar asked Jun 14 '12 02:06

Sergey Romanov


3 Answers

I found this article with some interesting ways:

1) without parameters use an empty array:

// Using a route with the name "register" and the route "/register"
echo $this->url('register', array(), array('force_canonical' => true)); 
// Output: http://mydomain.com/register

2) note the differences between:

echo $this->serverUrl(); 
// Output: http://mydomain.com

and

// Current URL: http://mydomain.com/register
echo $this->serverUrl(true); 
// Output: http://mydomain.com/register

3) starting from the route

// The "register" route has the following route: /register
echo $this->serverUrl($this->url('register')); 
// Output: http://mydomain.com/register
like image 156
Ivan Buttinoni Avatar answered Oct 23 '22 08:10

Ivan Buttinoni


You can use the option force_canonical on the router. All router options go into the third parameter of the url helper:

url($route, $params, $options)

So you can so something like this:

$this->url('myroute', array('id' => 123), array('force_canonical' => true))
like image 35
Jurian Sluiman Avatar answered Oct 23 '22 08:10

Jurian Sluiman


There is a Zend\View\Helper\ServerUrl to create full url in zend view. Try below code in your view template.

<?php echo $this->serverUrl()?>
like image 14
AlloVince Avatar answered Oct 23 '22 08:10

AlloVince