13

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?

4 Answers 4

45

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))
2
  • This functionality isn't referenced anywhere in the documentation and I think it's quite a common thing. Do you know how to also force it to use https for instance? I would have expected to use something like 'scheme' => 'https' as a parameter or option but it doesn't seem to be that easy Commented Jul 3, 2013 at 15:08
  • @DominicWatson I am not sure if there's an option to set the scheme. I create a https url by using Zend\Url\Http like this example: stackoverflow.com/questions/20122535/… Commented Nov 22, 2013 at 10:04
26

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
1
  • YOU ARE AWESOME DEAR, IT WORKS LIKE CHARM :)
    – Kamlesh
    Commented Oct 9, 2019 at 13:10
14

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()?>
1
  • 3
    The question is about generate full url in the view. But if someone get here looking for how to do it in the controller, here it is: $this->getRequest()->getServer('HTTP_HOST') . $this->url()->fromRoute('carrinho/compra-efetuada') Commented Feb 5, 2013 at 0:33
0

If you want to set base URL globally, you can do it using onBootstrap method:

$e->getApplication()->getMvcEvent()->getRouter()->setBaseUrl($baseUrl);

In this case Navigation helpers would also use it.

To fetch current base URL use ServerUrl helper as described in this thread:

$serverUrl = $e->getApplication()->getServiceManager()->get('ViewHelperManager')->get('ServerUrl');
$baseUrl = $serverUrl->__invoke();
1
  • "$serverUrl->__invoke();" ?? Isn't "__invoke()" made to be able to run it as "$serverUrl()" ?
    – Zippp
    Commented May 23, 2017 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.