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?

7
  • 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? Commented Apr 19, 2012 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 Commented Apr 19, 2012 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. Commented Apr 19, 2012 at 11:39
  • after i do what you said, the effective path becomes this: localhost/codeigniter/index.php/admin/application/views/css/… Commented Apr 19, 2012 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? Commented Apr 19, 2012 at 11:45

4 Answers 4

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

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

Comments

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

2 Comments

Yes you should place all your static files outside application folder say assets and refer to the in your view files using base_url()
I'm going to +1 this because you have the best name ever!!
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.

4 Comments

Can you please elaborate a standard and safe practice for this. i am new to codeignitor. Thanks.
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')
thanks :) i am following this: stackoverflow.com/questions/6630770/…
Hope that helped. Good luck :)
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

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.