40

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

Muhammad Usman
  • 12,439
  • 6
  • 36
  • 59
Hai Truong IT
  • 4,126
  • 13
  • 55
  • 102

5 Answers5

95

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" />
Muhammad Usman
  • 12,439
  • 6
  • 36
  • 59
  • if i only using $this->config->base_url("localhost/mysite"); in my controller but still get error `Fatal error: Call to undefined function base_url()` what is wrong here? I using CodeIgniter_2.1.4 – DeLe Aug 16 '13 at 01:37
9

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'); 
KutePHP
  • 2,206
  • 6
  • 36
  • 54
7

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();
birderic
  • 3,745
  • 1
  • 23
  • 36
4

Just load helper class

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

thats it.

Mischa
  • 42,876
  • 8
  • 99
  • 111
0

You need to add url helper in config/autoload

$autoload['helper'] = array('form', 'url', 'file', 'html'); <-- Like This

Then you can use base_url or any kind of url.