Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get base url in CodeIgniter 2.*

In config.php

$config['base_url'] = 'http://localhost/codeigniter/';

In View

<link rel="stylesheet" href="<?php base_url(); ?>css/default.css" type="text/css" />

=> Error: Call to undefined function base_url(); Help me

like image 546
Hai Truong IT Avatar asked Sep 21 '11 16:09

Hai Truong IT


People also ask

How to find base URL in CodeIgniter?

The base URL of the site can be configured in application/config/config. php file. It is URL to your CodeIgniter root. Typically, this will be your base URL, with a trailing slash e.g.

How can change base path in CodeIgniter?

$config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/mysite/"; This configuration will work in both localhost and server.


3 Answers

To use base_url() (shorthand), you have to load the URL Helper first

$this->load->helper('url'); 

Or you can autoload it by changing application/config/autoload.php

Or just use

$this->config->base_url(); 

Same applies to site_url().

Also I can see you are missing echo (though its not your current problem), use the code below to solve the problem

<link rel="stylesheet" href="<?php echo base_url(); ?>css/default.css" type="text/css" /> 
like image 151
Muhammad Usman Avatar answered Oct 02 '22 19:10

Muhammad Usman


I know this is very late, but is useful for newbies. We can atuload url helper and it will be available throughout the application. For this in application\config\autoload.php modify as follows -

$autoload['helper'] = array('url'); 
like image 44
KutePHP Avatar answered Oct 02 '22 21:10

KutePHP


You need to load the URL Helper in order to use base_url(). In your controller, do:

$this->load->helper('url');

Then in your view you can do:

echo base_url();
like image 26
birderic Avatar answered Oct 02 '22 21:10

birderic