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?
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);