Zend Framework 2 : Get Server URL in Controller

Zend Framework 2 - Get Server URL in Controller

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() or $this->baseUrl() 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 :

$helper = new Zend_View_Helper_ServerUrl();
$baseUrl = $helper->serverUrl(false);

Or

$helper = new Zend_View_Helper_ServerUrl();
$baseUrl = $helper->serverUrl(false);