site_url() and base_url() in CodeIgniter, Difference and when to Use

site_url() and base_url(), both are useful functions in CodeIgniter. However, developers get confused about what is the difference between them and when to use site_url() or base_url(). In this article, we’re explaining both and use scenarios.

Both functions are available in Codeigniter URL Helper. Once the helper is loaded, we can use them to load resources or produce internal links in the application. But one must understand the right use case to let the site work in the correct manner.

site_url() and base_url() in CodeIgniter

Also, please know that this article is for CodeIgniter version 3. Additionally, if you want to integrate login with Facebook, Google, etc. in CI then check the article All in One Social Login.

Loading URL Helper in CodeIgniter

First of all, you need to load the URL Helper manually before using them. You can add the code $this->load->helper('url'); either in the function or the controller constructor. However, where you call the loader, impacts the scope of the Helper.

Mostly we do use URL Helper functions across multiple places in the application files. So it is best to load the Helper globally using Codeigniter autoload file. Then we can use site_url() and base_url() in any function, controller, view as well as any library or helper we create or extend.

To autoload the Helper in the CI version 3, open the application/config/autoload.php file. Further, around line #92, add ‘url’ to the $autoload[‘helper’] array. So the line should look like $autoload['helper'] = array('url');.

Understanding base_url()

First, let us understand what are base_url() as well as site_url(). base_url() is the URL of your CodeIgniter root or the location of the index.php file along with a trailing slash appended to the end of the URL.

If the domain name is example.com and the website is using HTTPS as well as www. Further, the index.php file exists in the public_html directory then the base_url() would be https://www.example.com/.

For localhost, if project files reside in the htdocs/testsite directory then the base_url() in CI 3 would be like http://localhost/testsite/. We need to put that for $config[base_url] variable inside application/config/config.php file.

Understanding site_url()

site_url() uses base_url() and can be understood with the following statement:

base_url() + index_page (as defined in the config file) + path (any) + url_suffix (as defined in the config file)

So the following code explains different site_url() based on the parameter specified.

So you see that base_url() remains same for a given path. While site_url() varies with those two parameters. Also if the index_page and url_suffix parameters are empty, both site_url() and base_url() become the same.

When to Use base_url() or site_url()

The simple answer is to use base_url() to load resources like images, JS files, stylesheet files as well as fonts, etc. Since neither they need index.php in the middle to process nor any URL suffix.

For building internal links to your site in CodeIgniter, use the site_url function(). For example <a href=”<?php echo site_url(‘controller/method’); ?>” and it will generate a proper URL as per your configuration.

Additionally, remember that the anchor() and redirect() functions provided in CI, do use site_url() function internally. So no need to put that manually.

In this manner, by removing index.php and using ‘/’ as URL suffix, your CI website can have a trailing slash at the end of URLs like WordPress. However, for adequate working, some redirection rules in the .htaccess file are also necessary.

Hope you have clear the difference between both two. Have any questions or feedback? Write to us using the comment form.

You Might Interested In

1 COMMENT

Leave a Reply