Getting the server url / domain in ZendFramework

November 24th 2012

Sometimes, you need to be able to retrieve the current server url / domain from within your application dynamically. Doing so allows you to easily move code base from domain to domain without too many things going horribly wrong.

There are 2 places within ZendFramework you might need to do this. The first is from within the controller action, and the second is from within the view.

Here's how to get the Server URL in ZendFramework from within the controller:

PHP Code:
<?php
class IndexController extends Zend_Controller_Action
{
    public function 
indexAction()
    {
        
$url $this->getRequest()->getScheme() . '://' $this->getRequest()->getHttpHost();
    }
}

Here's how to do it from within the view:

PHP Code:
<?php

echo $this->serverUrl();

?>

Payal commented on Oct 21st 2013

I feel sastified after reading that one.

Goutam Chakraborty commented on Jan 7th 2014

Its help me..Many many thnaks