【问题标题】:css is not loading in codeigniter 4CSS未在codeigniter 4中加载
【发布时间】:2020-01-27 20:49:01
【问题描述】:

我将我的 css 和 js 保存在 codeigniter 4 根目录的 assets 文件夹中,我正在尝试使用这一行加载我的 css

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

但是我的 CSS 没有加载。 这条线工作正常 codeigniter 3 。在此先感谢。

【问题讨论】:

标签: css codeigniter assets


【解决方案1】:

base_url() 函数已经打印了一个 '/' 。所以删除php标签后的斜线。像这样:

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

【讨论】:

    【解决方案2】:

    我曾经遇到过这个问题,因为 codeigniter 4 像你一样链接 css 和 js 文件。 ci4似乎在这个url中添加了当前路径,结果变成[current_path]/[base_url]/assets/css/style.css,也就是http://localhost/ci/home/localhost/ci/assets/css/style.css。

    为了解决这个问题,我只是在“application/config/config.php”的base_url中添加了“http://”。像这样:

    $config['base_url'] = 'http://localhost/ci';

    【讨论】:

      【解决方案3】:

      为了使用base_url(),您必须首先加载 URL Helper。这可以在application/config/autoload.php

      中完成
      $autoload['helper'] = array('url');
      

      或者,在您的控制器中手动加载:

      $this->load->helper('url');
      

      base_url() 函数默认返回一个斜杠。所以不需要在 base_url() 之后添加'/'。所以你的样式表链接应该如下所示:

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

      或者,你也可以这样写:

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

      您可以从这里获得帮助https://codeigniter.com/user_guide/helpers/url_helper.html

      【讨论】:

      • 恐怕你的答案是 CodeIgniter 3 而不是 CodeIgniter 4。
      猜你喜欢
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      相关资源
      最近更新 更多