The nerdy adventures of...

Phil Sturgeon


My name is Phil Sturgeon and I program stuff in PHP & CodeIgniter.
I write about Linux, Git, jQuery and all sorts of other computery things!

Asset handling in CodeIgniter with the BASE tag

Posted in CodeIgniter at Sep 17, 2009

There are many suggested ways to handle your assets (CSS, images, JavaScript, etc) in CodeIgniter including my very own Asset library. Some people use paths relative to the web root like this:

<img src="/image/logo.gif" alt="Logo" />

This method works fine if your application will only EVER exist in the web root. If that works for you, fine, no need to read on.

Those that need their assets to be found wherever their CodeIgniter application happens to sit (root, sub-directory, sub-domain) can use libraries, helpers or add base_url() in front of all refferences to an asset. This does work but seems a bit overkill for something that can be handled with HTML.

My new evil scheme? Use the lesser-known HTML tag <base> to get all of our links and refferences working perfectly. This should go into the <head> of your document.

<base href="<?=base_url();?>">

Now all your relative links will be relative to your base_url. Brilliant!

<img src="images/logo.gif" alt="Logo" />

That will work fine, assuming your images are stored in public_html/images. Use some common sense on that.

Comments

User comments
  • Gravatar Eric Barnes

    Sep 22, 2009

    Great suggestion! I have been using this method for years now. Another thing I do is wrap this inside an if that checks for ssl. Then on ssl pages all your assets are called correctly.

  • Gravatar Jeremy

    Sep 22, 2009

    Wow. How did I not know this? *bangs head on desk repeatedly*

    Thanks for sharing and saving me tons of time in the future!

  • Gravatar Briandhall

    Sep 22, 2009

    Fantastic, thanks for posting, and thanks to you too Eric for that if switch. So much easier than how I've been handling it!

  • Gravatar Lat233

    Feb 24, 2010

    Hi
    Have you test with your server configured in Rewrite mode?

  • Gravatar Phil Sturgeon

    Oct 30, 2009

    Teo: I think that was just GitHub having a funny 5 minutes. Seems ok to me.

  • Gravatar Jamie G

    Nov 02, 2009

    I have just started out using CI and instantly came up against this issue. A clean solution in a fantastic framework. Thanks Phil!

  • Gravatar Dustin

    Nov 04, 2009

    It's giving the cURL thing again... odd. Looks like you've done a great job with the Asset library and I look forward to giving it a whirl. Thank you!

    (As a side note, this captcha is pretty aggrivating... apparently I'm not a human!)

  • Gravatar Zonkers Terwillager

    Oct 20, 2009

    Neat! A hiccup to note: If you're using page anchors: href="#fu", they'll point out the base url page, not internally on your page.

  • Gravatar Nick

    Oct 22, 2009

    As a newcomer to PHP frameworks, I refuse to complicate things that were previously simple w/ pure HTML.. so I was horrified at the thought of adding base_url function calls to every asset tag. This is a huge help and a huge relief. Thanks a ton.

  • Gravatar Teo Jurado

    Oct 27, 2009

    Sorry, but I can't download this Library, The link is correct (I supose), but ony download the cURL Controller....

    Please... Correct This...

    Thanks

  • Gravatar Trae Regan

    Oct 04, 2009

    Another great CI time-saver from Phil. Thank you!

Post a comment