'Zend Framework 2 : get server URL in controller without using serverUrl()'

Sometimes you need to be able to get current server URL inside controller in your Zend Framework 2 application. As you might know, you won’t be able to use $this->serverUrl() method in the controller as it’s a part of helper.

So, here is the code that I am using to get server URL in controller :

1
    $server_url = $this->getRequest()->getUri()->getScheme() . '://' . $this->getRequest()->getUri()->getHost();

Probably it’s not the best solution but it works for now. Please check the other solutions below:

Other solutions from the comments below

1
2
    $helper = new Zend\View\Helper\ServerUrl();
    $url = $helper->__invoke(true);
1
2
    $this->getEvent()->getRouter()->assemble(array(), array('name'=>'home', 'force_canonical'=>true));
    // WHERE default is the name of your route, "default" or whatever you use
comments powered by Disqus