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?
Use Router::url('/', true) anywhere in your app. Specifically in the View, you can use $this->Html->url('/', true) (or any other Helper in fact, the Helper::url method is inherited by all Helpers), which is just a wrapper for the above Router method.
base_url() is the URL of your CodeIgniter root or the location of the index. php file along with a trailing slash appended to the end of the URL. If the domain name is example.com and the website is using HTTPS as well as www. Further, the index.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With