【问题标题】:Correct way to load CSS in a codeigniter view在 codeigniter 视图中加载 CSS 的正确方法
【发布时间】:2017-10-18 01:48:55
【问题描述】:

我在配置中的基本网址是:$config['base_url'] = 'http://localhost/ci/';

我的 CSS 文件夹位于根级别(所以 ci)。

在我尝试过的视图中:

<head>
    <style> @import url('<?=base_url()?>/css/styles.css'); </style>
</head>

<head>
    <link rel="stylesheet"  type="text/css" href="<?php echo base_url()?>css/styles.css">
</head>

两者都失败了。使用 Codeigniter 框架导入 CSS 文件的正确方法是什么

这是我的 .htaccess 文件的样子:

    <IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

【问题讨论】:

  • 检查浏览器开发者工具的 404。
  • 您确定可以从浏览器访问吗? http://localhost/ci/css/styles.css
  • @KeithChason 是的!
  • 接下来我会按照@Scriptonomy 所说的去做,你怎么知道它“不起作用?”
  • 我认为是 CSS 问题而不是 CodeIgniter

标签: php css codeigniter


【解决方案1】:

我使用这种方法:

文件夹结构:

example.com
|_ 应用
|_ 资产
|_ 系统

CI 2.x

我设置了$config['base_url'] = '';,这将适用于本地服务器生产服务器

CI 3.x

警告:您必须设置此 ($config['base_url']) 值!所以我使用下面的代码来区分本地服务器生产服务器

if ($_SERVER["HTTP_HOST"]=='ex.ample')
    $config['base_url'] =  'http://ex.ample/';
else
    $config['base_url'] =  'http://example.com/';

(我在本地服务器上使用名为ex.ample 的虚拟主机,根据您的需要更改)

在我的文件夹资产中我存储assets/css/style.css

并在我看来用以下方式调用它:

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

【讨论】:

  • 您需要在 codeigniter 3 > 版本中设置基本 url,如上面评论中所述。
  • @wolfgang1983:前导斜杠绝对确保您加载的是相对路径,如果您将其拿走,CI 2.x 可能会中断...
  • 我像 href="&lt;?php echo base_url('assets/css/style.css');?&gt;" 一样使用它,因为你的基本网址已经准备好了 /
  • @wolfgang1983 是的,这是另一种正确的方法,但您需要输入更多内容:)
  • 当我尝试上述方法时:调用未定义的函数 base_url()
【解决方案2】:
<link rel="stylesheet"  type="text/css" href="<?php echo base_url()?>css/styles.css">

base_url 末尾缺少分号

此代码可以确保您提供正确的路径

你也可以使用

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多