为什么浏览器显示 404 视图而不是在此 Codeigniter 3 应用程序中加载样式表?
Why does the browser show the 404 view instead of loading a stylesheet in this Codeigniter 3 app?
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 CMS 4. 我已决定为其添加主题。该应用程序不是 HMVC,只有 MVC。
themes 目录在 application 之外,如下图所示:
在 themes 中,我有主题目录(当然),其中包含“主视图”,layout.php
:
我如何使用主题视图
在 application/core
中,我添加了一个 MY_Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
function theme_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(FCPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->load->theme_view('/themes/caminar/', 'layout', $data);
}
问题
在 layout.php
中,我包含了主题的样式表:
<link rel="stylesheet" href="<?php echo site_url('themes/caminar/assets/css/main.css')?>">
文件 main.css
确实位于 http://example.com/themes/caminar/assets/css/main.css
但是,浏览器没有加载 CSS 文件,而是加载了 404 视图。
我做错了什么?
您在加载资产时使用的是 site_url 而不是 base_url。
站点 url 用于链接您的应用程序,通常是控制器功能。
base_url用于资产。
所以将路径更改为:
<link rel="stylesheet" href="<?php echo base_url('themes/caminar/assets/css/main.css')?>">
如果这不能解决问题,那么您的服务器中可能有某些东西阻止了它。在这种情况下,如果您使用的是 apache,请向我们展示您的 .htaccess 或者如果您使用的是 nginx,请向我们展示您对所述虚拟主机的配置。
如果是 apache,您可以尝试将其添加到您的 .htaccess。
RewriteCond !^(index\.php|assets|themes|images|js|css|uploads|favicon.png)
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 CMS 4. 我已决定为其添加主题。该应用程序不是 HMVC,只有 MVC。
themes 目录在 application 之外,如下图所示:
在 themes 中,我有主题目录(当然),其中包含“主视图”,layout.php
:
我如何使用主题视图
在 application/core
中,我添加了一个 MY_Loader.php
文件,其中包含以下内容:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
function theme_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(FCPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
}
在我的 Posts 控制器的 index()
方法中,我加载了传递数据的视图:
public function index() {
//more code here
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->load->theme_view('/themes/caminar/', 'layout', $data);
}
问题
在 layout.php
中,我包含了主题的样式表:
<link rel="stylesheet" href="<?php echo site_url('themes/caminar/assets/css/main.css')?>">
文件 main.css
确实位于 http://example.com/themes/caminar/assets/css/main.css
但是,浏览器没有加载 CSS 文件,而是加载了 404 视图。
我做错了什么?
您在加载资产时使用的是 site_url 而不是 base_url。
站点 url 用于链接您的应用程序,通常是控制器功能。
base_url用于资产。
所以将路径更改为:
<link rel="stylesheet" href="<?php echo base_url('themes/caminar/assets/css/main.css')?>">
如果这不能解决问题,那么您的服务器中可能有某些东西阻止了它。在这种情况下,如果您使用的是 apache,请向我们展示您的 .htaccess 或者如果您使用的是 nginx,请向我们展示您对所述虚拟主机的配置。
如果是 apache,您可以尝试将其添加到您的 .htaccess。
RewriteCond !^(index\.php|assets|themes|images|js|css|uploads|favicon.png)