How to get base url in CodeIgniter 2.*

Hai Truong IT picture Hai Truong IT · Sep 21, 2011 · Viewed 117.2k times · Source

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

Answer

Muhammad Usman picture Muhammad Usman · Sep 21, 2011

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" />