codeigniter Something More About base_url


Example

What happens if I don't set base_url ?

You will not get any Impotency error to set this and proceed. You can continue without setting, but you should know about HTTP header injection


If I did't set it what will show up?

You will get http://[::1]/ instead of your actual URL.


What does this mean http://[::1]/ ??

This is temporary URL which set by CI by Default. This will point the root of your document.

::1 - Server address (localhost) Read More about this


How to set proper base_url()??

Base URL should always point to root of your project folder. (outside application folder)

$config['base_url'] = 'http://localhost/path/to/project'; # If localhost
$config['base_url'] = 'http://stackoverflow.com/'; # If live
$config['base_url'] = 'http://stackoverflow.com/documentation'; # If live & inside subdomain (assume documentation is subfolder/subdomain)

How to use base_url()??

Most common use is to find the right path to your js or css files.

<link rel="stylesheet" href="<?php echo base_url('styles/style.css');?>" />
<script src="<?php echo base_url('vendor/jquery/jquery.min.js');?>"></script>

Adding the code above in your view will produce HTML as below:

<link rel="stylesheet" href="http://localhost/path/to/project/styles/style.css" />
<script src="http://localhost/path/to/project/vendor/jquery/jquery.min.js"></script>

Links

  1. URL Helper