base_url in CakePHP

Musa picture Musa · Nov 28, 2010 · Viewed 86.9k times · Source

In most web applications we need global var base_url. In cakephp to get base_url currently i put the following code on beforeRender method in app_controller.php

function beforeRender(){
    $this->set('base_url', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}

Is there any alternative? Means is there any global variable available to get the base url rather than this?

Answer

RabidFire picture RabidFire · Nov 28, 2010

Yes, there is. In your view, you may access:

<?php echo $this->webroot; ?>

Also, your host information is stored in the $_SERVER['HTTP_HOST'] variable in case you want that.

In your controller, if you want full URLs, use this:

Router::url('/', true);