【问题标题】:CSS Not Found Codeigniter 4CSS 未找到 Codeigniter 4
【发布时间】:2021-07-22 13:56:06
【问题描述】:

我在 Codeiginiter 4 中包含 css 时遇到问题。

当我访问此路由时 localhost/my_project/user_role 成功包含所有 css。 但是,当我访问此路由 localhost/my_project/user_role/add 时出现 2 个错误,即 css net::ERR_ABORTED 404(Not Found)

我在我的网络检查器中查看网络选项卡,包括 bootstrap.min.css 和 app.min.css,状态代码为 200,但仍显示未找到错误

这就是我加载 css 文件的方式

这是我的 htaccess 文件

# Disable directory browsing
Options All -Indexes
 
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
 
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
 
    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    # RewriteBase /
 
    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
      RewriteRule ^ %1 [L,R=301]
 
    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
 
    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
 
    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
 
<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>
 
# Disable server signature start
    ServerSignature Off
# Disable server signature end

我该如何解决这个问题?请帮忙

【问题讨论】:

  • 将 .htaccess 添加到您的问题以及您在视图中加载 css 的代码,以便我们查看您是如何加载它的。
  • @marcogmonteiro 请重新阅读我的问题。我已经包含了它
  • 尝试将加载 css 的方式更改为 base_url('path/to/your/file.css')。如果这不起作用,在您的网络选项卡中,您得到的 404 是来自 codeigniter 还是来自 apache/nginx?
  • 你的base_url() 是什么?检查配置,我认为您在基本 url 之后包含额外的斜杠以加载 css。只是交叉检查它

标签: jquery css codeigniter codeigniter-4


【解决方案1】:

新 CI4 的结构与之前的版本不同。 现在有一个公用文件夹。我建议您将前端资产放入公共文件夹中,这样您的加载会更简单,并且您不必包含 base_url 即:

<link rel="stylesheet" href="assets/css/style.css">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2015-08-29
    • 2013-03-27
    相关资源
    最近更新 更多