»
S
I
D
E
B
A
R
«
Using Images, Scripts, CSS inside Code Igniter
May 8th, 2008 by Nitesh

As web developers, we often run into problems while handling various assets such as images, scripts and css files. To make it easier to access and call such assets from within the Code Igniter framework, here is an implementation of the Asset Library, which we have been using at Wallwisher. Hope you find it useful.

Structure

In parallel to the Code Igniter system folder in your implementation, place a new folder called assets. Inside this folder, feel free to describe new assets such as images, scripts, styles and others necessary for your implementation. You need the following files to implement the library:

  1. Configuration File: to be placed in system/application/config/ 
  2. Helper File: to be placed in system/application/helpers/
  3. Library File: to be placed in system/application/libraries/

Examples

Here are a few examples which might help you in understanding the usage of this library:

  1. $new_image = ‘.’.asset_url(‘example.jpg’,'images’);
  2. $CI =& get_instance();   $new_css = $CI->assets->get_asset_from_name(‘css’,$name);

4 Responses  
  • Safique writes:
    July 21st, 2008 at 4:05 am

    Example could be more fruitful if implementation was also shown for us(beginner)

  • sky writes:
    December 25th, 2008 at 2:09 pm

    can you give me some script for implementation in controller n view??

    Thank u

  • Admiral Potato writes:
    January 6th, 2010 at 4:36 am

    Please forgive me if i’ve completely misunderstood the objective of this post, but…
    The solution to this issue ( as I understand it ) is REALLY, REALLY easy.
    The solution is the HTML tag.
    Check out some documentation on it here:

    http://www.w3schools.com/TAGS/tag_base.asp

    I implemented the tag like this for my site, and if you’ve set the $config['base_url'] variable in your system/application/config/config.php file, it should work for you too.

    <base href="config->item(‘base_url’)?>” />

    Basically, you just put this in the of your view document before your HTML tells the browser to load anything else, and no matter how complex the URL gets, the browsers viewing your pages will look for all of their assets relative to that location.

    Just remember that if you are going to be using any assets in the folder that your system folder is sitting in, and you’re using the stock .htaccess file to remove the index.php portion of your URLs, you will need to change that .htaccess file to specifically allow browsers access to the real files and folders you will be placing in your $config['base_url'] directory.

    Example? This:
    RewriteCond $1 !^(index\.php|images|robots\.txt)

    Might turn into this:
    RewriteCond $1 !^(index\.php|images|img|javascript|fonts|content|images|styles.css|layout.html|robots\.txt)

    Again, I apologize if I was completely off-topic and didn’t understand the intent or goal of this post, but I hope that this was able to help /someone/ out there!

  • temptemp writes:
    February 7th, 2010 at 7:05 am

    I already looked everywhere and found related answers over and over BUT they all are incomprehensive for beginners. Meaning….inconsistency and abstract. Sorry folks but really very disappointing. I think you should specify what files are to changed (ie, config.php, etc), where and what to put. Or you can just post a very simple webpage with such example – i think that won’t be a burden. LIKE (you can make a php for controller and view [except maybe for model since you need more knowledge on database] and put css and instruct where to place css, what codes are to be placed in .phps to link the css…..), isn’t that simple?


Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa