codeigniter 3 - missing css files

2019-08-14 04:22发布

问题:

Problem description:

when i navigate to some pages in my web app, the css files are not being loaded / found. I'm getting 404 not found errors and I can't see why. It must be something simple that I'm not seeing.

Specifically, when I navigate to default page, it works (calls the index() method below) but when I try to run the getallswitches() page, none of the css files are loaded.

Code

I have the following CI controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Objects extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->model('objects_model');
        }

        public function index()
        {
                $data['main_content'] = 'default';
                $this->load->view('includes/template',$data);
        }

        public function getallswitches()
        {
                $switches= $this->objects_model->get_all_switches();
                $data['switches']= json_encode($switches);
                $data['main_content'] = 'switches';
                $this->load->view('includes/template',$data);
        }
}

I'm using a templating system to load my views. the template file looks like this:

<?php
        $this->load->view('includes/header');
        $this->load->view($main_content);
        $this->load->view('includes/footer');
?>

Inside my header.php file, I have logic to add css files like so:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">
    <title>test app</title>
    <link href="../application/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="../application/assets/css/justified-nav.css" rel="stylesheet">
    <link href="../application/assets/css/dashboard.css" rel="stylesheet">
    <script src="../application/assets/js/ie-emulation-modes-warning.js"></script>
  </head>

Any tips on what I might be messing up? I've been looking at this too long.

EDIT 1

This is what the URL looks like when it fails:

http://localhost/testapp/index.php/application/assets/css/bootstrap.min.css

And when it works:

http://localhost/testapp/application/assets/css/bootstrap.min.css

回答1:

Use absolute path for you resources.

<link href="/application/assets/css/bootstrap.min.css" rel="stylesheet">

or make use of base_url() function:

<link href="<?=base_url();?>application/assets/css/bootstrap.min.css" rel="stylesheet">

But generally:

Don't put your assets into application folder. Better place assets folder inside root of your app, or even better separate public from app and system folder, move assets to public folder. https://stackoverflow.com/a/32346618/5173560



回答2:

It might be some problem with your .htaccess file of your root directory.

Put this code in your .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]