CSS 在 codeigniter 中使用段 URI 时未加载

CSS is not loading when using segment URI in codeigniter

为什么在 codeigniter 中使用段 URI 时 CSS 没有加载 但是当我删除该段时它运行良好?

这就是我 link css 文件的方式

<title>TITLE</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- bootstrap -->
<link href="public/css/bootstrap.css" rel='stylesheet' type='text/css' media="all" />
<!-- //bootstrap -->
<!-- Custom Theme files -->
<link href="public/css/style.css" rel='stylesheet' type='text/css' media="all" />

<script src="public/js/jquery-1.8.3.min.js"></script>

控制器

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

class profiles extends CI_Controller
{

        public function index()
{      
        $user_id = $this->uri->segment(2, 9);

        $this->load->model('user_model');
    $this->load->model('count_model');
              $data   = array();


    $result =  $this->user_model->read_user_information_profile($user_id);        

     $data = array(
    'username' => $result[0]->username,
    'fname' => $result[0]->fname,
    'lname' => $result[0]->lname,

        );
    $this->load->view('profiles_view', $data);
    }
 }

提前致谢

因为您正在使用相对 URL 来加载您的 CSS,它试图从 /dir/profiles/public/css/style.css 而不是 /dir/public/css/style 加载它。 css 因此它无法加载的原因。

您可以在 CSS 中使用绝对 URLs:

<link href="<?php echo base_url('public/css/style.css'); ?>" rel='stylesheet' type='text/css' media="all" />

或者在尝试加载任何 CSS 或 JS 之前设置基本标记:

<base href="<?php echo base_url(); ?>" />

请参阅:http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url,请注意,如果您尚未加载 URL 帮助器,则可能需要加载。

以这种方式更改加载:

<!-- bootstrap -->
<link href="<?php echo base_url(); ?>css/bootstrap.css" rel='stylesheet' type='text/css' media="all" />
<!-- //bootstrap -->
<!-- Custom Theme files -->
<link href="<?php echo base_ur;(); ?>css/style.css" rel='stylesheet' type='text/css' media="all" />

<script src="<?php echo base_url(); ?>js/jquery-1.8.3.min.js"></script>

您不需要配置 base_url。它会自动检测