views:

26

answers:

2

I have started learning Code Ignitor and I am very impressed, and have had an easy time. The only issue I haven't been able to figure out is how to reference images, javascript, and CSS. I have each as a folder (css,js,img) in the root folder. This works fine for my default view, but breaks with the sub views. How am I supposed to handle this the "code ignitor" way?

A: 

there is no "codeigniter way" in HTML, because codeigniter is PHP server-side framework, while your problem is client side related

just always use an absolute path to your resources.

/css/styles.css
/js/query.php
/img/news/566.gpg

will always be fine

note that / stick at the left. that means "root" of the site, and a path, built from the root, called "absolute", which mean "always works from anywhere".

Always use absolute path for both filesystem operations and virtual server resources

Col. Shrapnel
Thank you, I didn't realize using a slash in front would do things relative to the doc root. I have messed with other frameworks like Zend, and they have resource helpers. I didn't know if Code Ignitor did as well.
Josh
And this is going to depend on your htaccess (most people use one to get rid of having index.php in the url). There's two main types, one that allows certain folders to be served and everything else to go through index.php, and one (my personal preference) where all files that don't exist on the disk go through index.php, that way you don't have to worry about which folders things are in.
Matthew
@Matthew no, this is going **not** to depend on your htaccess. that's what absolute paths are for
Col. Shrapnel
@Josh I am sure CI has such a helper as well.
Col. Shrapnel
+1  A: 
$this->load->helper('html');
echo img('absolute_path_to_image_relative_to_document_root');

echo link_tag('absolute_path_to_style_sheet_relative_to_document_root');

You are free to handcode these tags as in the other answer but if you change your url, or want to install the same code on a different url, you have to change them all by hand. This way all you have to do is change the configuration.

If you use the helper function, you do not use the leading slash either as CI will stick it in for you. what you will end up with is a true absolute url of the form http://mysite.com/images/pic1.jpg from the input echo img('images/pic1.jpg');

aaronasterling