0

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.

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Yup, I know about this. Is that the only advantage on using the helper?
It's the only one that I know of.
I guess the other benifit is it will return a fully qualified url as opposed to a site absolute reference. Like for permalinks and syndication.
0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.