Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base_url in CakePHP

Tags:

php

cakephp

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?

like image 305
Musa Avatar asked Nov 28 '10 19:11

Musa


People also ask

How to get base URL in CakePHP 3?

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.

What is Base_url () in PHP?

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.


1 Answers

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); 
like image 65
RabidFire Avatar answered Oct 05 '22 02:10

RabidFire