[php] 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

This question is related to php codeigniter

The answer is


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.


Just load helper class

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

thats it.


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();

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');