0

i'm developing a web application with php codeigniter. in views folder, i created header.php that contains css and js and etc files and footer.php for footer of pages. in default controller, when i use $this->load->view('header'), it's Ok. but when i use this command in other pages, css files can not load. in inspect element i've got errors that says in css file path, codeigniter added index.php/ctrl_name to this address.

this is path of my css file:

http://localhost:9000/CRM/index.php/home/assets/css/materialize.min.css
3
  • Please post more of your code. Commented Jun 26, 2017 at 12:30
  • Have you set your base url to $config['base_url'] = 'http://localhost:9000/CRM/'; You need to. Commented Jun 26, 2017 at 22:58
  • 1
    Assets must be out side of application folder. Commented Jun 26, 2017 at 22:58

6 Answers 6

6
  1. Load url helper in application/config/autoload.php $autoload['helper'] = array('url');

  2. Set base_url config in application/config/config.php $config['base_url'] = 'http://localhost/MyProject';

  3. In header.php, include js and css like this: <link href="<?=base_url();?>assets/css/materialize.min.css" rel="stylesheet"> Good luck!

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

3 Comments

TNX bro! i exactly do this. but not working for other controllers.
Can you show me your directory order? where do you put that assets folder?
i put this folder in the root of project.
0

I would suggest you to put all static files inside an "assets" folder inside your web root. Let's say you create a folder called "assets" with folders inside for the specific types, i.e. js, img, css.

Then always refer to them as in the following examples :

  • /assets/js/main.js
  • /assets/css/style.css
  • /assets/img/picture.jpg

2 Comments

i do this. for default controller it works fine, but for other controllers it add index.php to path of these assetes.
Then the files for sure are not in the correct place. If the files are in the webroot then the / will make the browser point there by default.
0

This is because you use relative URLs for your assets, probably something like 'assets/css/materialize.min.css'.

One possible solution is to use absolute URLs instead: base_url('assets/css/materialize.min.css')

An other solution would be to use a constant to store the path to your assets folder, then use that constant every time you include some asset:

define('ASSETS_URL', base_url('assets/'));

// or you can even define separate constants for CSS
define('ASSETS_CSS_URL', base_url('assets/css/'));

// then use it like this
// note the extra slash, base_url() strips trailing slashes
$path_to_css_file = ASSETS_CSS_URL . '/materialize.min.css';

1 Comment

Check the generated URL. It should give you some hints of what's wrong.
0

try this url

<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/materialize.min.css">

if your base url root directory and your assets folder same as root directory

2 Comments

i exactly do this. but in other controllers not working.
where is your assets folder and check url link in browser view page source
0

Please autoload url helper in autoload.php inside application->config and then link all file via base_url().Put all js and css files in assests folder and then check link code:

<link href="<?php echo base_url(); ?>/home/assets/css/materialize.min.css" rel="stylesheet" type="text/css" >

Comments

0
<link href="<?php echo base_url();?>assets/css/materialize.min.css" rel="stylesheet">

use this in your view files(header.php)

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.