4

I am new to codeigniter. I made a simple controller to load the three views: header, body and footer. My header contains links to several javascripts and CSS which have a relative paths. I kept all the css and js relative to file in views folder.

The problem is that the controller loads everything properly but images, js and css are not getting loaded.

This is the hierarchy of my view:

CSS(sub directory in views) JS (sub directory in views)

header (header view file) body (body view file) footer (footer view file)

ny idea?

bmrinal
  • 365
  • 3
  • 4
  • 10
  • 1
    How are you linking to these resources? Are you using relative or absolute URLs? Are you using CodeIgniter functions? base_url('css/styles.css'); for example? Woulld it be possible to show a little of your code where you're having the problem? – Dale Apr 19 '12 at 11:19
  • This is the line i am having in the header.php file of mine. And i have the CSS folder in the views directory. text.css lies in Views->css->text.css – bmrinal Apr 19 '12 at 11:38
  • 1
    You have to make the path relative to the index.php file so I would suggest changing the `href` attribute to `href="application/views/css/text.css"` if this doesn't work play about with it, it's definitely something along these lines. – Dale Apr 19 '12 at 11:39
  • after i do what you said, the effective path becomes this: http://localhost/codeigniter/index.php/admin/application/views/css/text.css – bmrinal Apr 19 '12 at 11:41
  • Hmm.. There are many way to set up CodeIgniter and I can't tell from the information you have provided how you set yours up. Are you using htaccess file to rewrite your urls? – Dale Apr 19 '12 at 11:45
  • i did this: ../../application/views/admin/css/reset.css THE PATH BECOMES CORRECT. BUT WHEN VISITED it is showing 403 forpidden error. – bmrinal Apr 19 '12 at 11:46
  • Has that worked for you? HI5 for getting there :-) – Dale Apr 19 '12 at 11:46

4 Answers4

6

It is not a good idea to put everything in your view file, as those folders are supposed to clean things up, putting them in view makes them messy again.

Assume this is your directories:

Localhost

  • Application
    • Controllers
    • Views
    • Etc..
  • System
  • Assets
    • Javascripts
    • CSS

To include any of javascripts, the path would be:

localhost/Assets/Javascripts/your_script.js

and obviously CSS would be

localhost/Assets/CSS/your_stylesheet.css

In order to make sure your implementation work everywhere,

<script src="<?=base_url('path/to/your/js/foo.js')>" type="text/javascript"></script>

would be the safest way

Jonathan Chow
  • 703
  • 7
  • 29
4

You need to put the files in the base directory e.g. the one containing the application and system folders.

Try using a folder called assests.

Also make sure your are using a leading slash so e.g.

href="/assets/css/style.css"

Note this will load from base URI

e.g. example.local/assets/css/style.css

Tom
  • 12,776
  • 48
  • 145
  • 240
Dale Hurley
  • 151
  • 7
  • Yes you should place all your static files outside application folder say `assets` and refer to the in your view files using `base_url()` – Broncha Apr 19 '12 at 19:03
  • I'm going to +1 this because you have the best name ever!! – Dale Apr 19 '12 at 21:11
2

I would suggest you to not keep your js/images/css files in the view folder.

Please refer to this past question. Where do I put image files, css, js, etc. in Codeigniter?

As to answer your question, it is like Dale said.

First you will need to check your .htaccess, and note where is your base directory.

You'll then have to provide a path link that is appropriate to your js/css folder. If you kept them in your views folder, it seems like you'll have to do this url

http://localhost/codeigniter/index.php/application/views/css/text.css

If I am not wrong, you are currently at

"http://localhost/codeigniter/index.php/admin"

trying to load the css files. If you use href="something", it will merely add on to your current URL. Which will provide "http://localhost/codeigniter/index.php/admin/something" which IMO is not what you're looking for.

Community
  • 1
  • 1
He Hui
  • 2,196
  • 4
  • 29
  • 46
  • Can you please elaborate a standard and safe practice for this. i am new to codeignitor. Thanks. – bmrinal Apr 19 '12 at 12:08
  • I am not a 'pro' either. What I normally do is this: I keep my files in `/Extras/img`, `/Extras/css`, `/Extras/js`. Hence, if I ever need to access any image files, I will do this: `base_url('Extras/img/foo.jpg')` – He Hui Apr 19 '12 at 12:11
  • thanks :) i am following this: http://stackoverflow.com/questions/6630770/where-do-i-put-image-files-css-etc-in-code-igniter – bmrinal Apr 19 '12 at 12:15
  • Hope that helped. Good luck :) – He Hui Apr 19 '12 at 12:15
0

The best way to link to css files is like this

echo link_tag('css/reset.css');

Note the link tag does not use the / while the hs file below does

Your css and js needs to be in the root directory

JS files

<script defer src="/media/javascripts/scrips.js"></script>

Also in the root. Of coruse your directory will be different

You cannot link to css files that are in the same view directory

http://codeigniter.com/user_guide/helpers/html_helper.html

Brad
  • 1,685
  • 1
  • 27
  • 38