kevinandrada
kevinandrada

Reputation: 499

Difference between using $base_url and not using it on CodeIgniter

So basically, what's the difference between the two approach:

<img src="<?php print base_url(); ?>img/test.png" />
<!-- and -->
<img src="/img/test.png" />

I'm having the same result on my website. Might as well remove the URL helper ($this->load->helper('url')) on my controller to cut extra process since they're just the same.

If there is an advantage of adding the helper at all, please let me know.

Upvotes: 0

Views: 226

Answers (4)

NwK
NwK

Reputation: 16

I've discovered another thing usin site_url(). It is the only way I found to set a / at the end of my urls. Eg : will result in http://domaine.tld/controller

On the other hand http://domaine.tld/controller/ hope that helps somebody

Upvotes: 0

imkrisna
imkrisna

Reputation: 853

There is no difference if you're using root path of domain.

It's kind like a feature that if you're using it you can migrate easier to any domain or inside directory path.

One more advantage is when you're developing API or RSS feed, the image path or anything else will be written as complete URL so the consumer can use it easily.

Upvotes: 0

GeoffCodesThings
GeoffCodesThings

Reputation: 858

In my experience with CodeIgniter, using base_url() is a best practice. It ultimately guarantees that your links will be correct (similar to the way adding ../ works when the folder you need is 1 level higher). Also I have had problems in the past with CodeIgniter adding the url in config[base_url] twice if I forgot to put http:// in it. Simple mistake, but it can throw the whole project off. So, basically the difference is that it guarantees the path is the right one, but isn't necessary at all times.

Upvotes: 0

Orangepill
Orangepill

Reputation: 24655

The difference is if you ever want to put your application in a subdirectory.

If what was / now becomes /application/ all your site root references will break.

It just makes your application free to be installed anywhere and won't require being on the site root.

Upvotes: 3

Related Questions