将 css 文件夹放在 codeigniter 的什么位置?

where to put css folder in codeigniter?

我正在使用 php 和 codeigniter。 我的 css 文件夹在这样的项目文件夹中 localhost/project/css/main.css 但我的 view.php 仍然无法访问那个 main.css 它没有给我任何错误。但是当我试图在控制台栏中显示 incept 元素时,它显示 css 文件的错误 404。在该位置可用时找不到资源

试试这个

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

这是我喜欢处理资产的方式。

我在 application/helpers/

中创建了一个名为 assets_helper 的助手
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('css'))
{
    function css($nom)
    {
        return '<link rel="stylesheet" href="' . base_url() . 'assets/css/' . $nom . '.css " type="text/css" media="screen" />';
    }
}

if ( ! function_exists('css_print'))
{
    function css_print($nom)
    {
        return '<link rel="stylesheet" href="' . base_url() . 'assets/css/' . $nom . '.css " type="text/css" media="print" />';
    }
}
//This is only the part that handle css as it is what's bothering you

这是我使用的完整助手:http://pastebin.com/ujETEXJ4

之后,在与 index.php 相同的级别,我创建了这些文件夹:

|-Application
|-System
|-index.php
|-Assets
    |- css
    |- sass
    |- images
    |- js

将您需要的所有 css 文件放入新的 css 文件夹中。

在我的 application/config/autoload.php 我添加了我的新助手

$autoload['helper'] = array('assets', ...);

最后,在我页面的页眉中:

<?php echo css('mycss'); ?> //I did not forgot the extension, it's how it works :)

最后会给出:

<link rel="stylesheet" type="text/css" href="http://www.example.com/assets/css/mycss.css" />

这样我就可以轻松地在我的代码中加载任何资源:

css('mycss'); //load css
css_print('mycss'); //css media="print"
js('myjs'); //load js
img('myimg.png') //img tag
img_url('myimg.png') //path to an image

编辑:

要使其正常工作,请确保您已在 application/config 中正确设置 base_url。php

$config['base_url'] = "http://localhost/myawesomesite/";
//No index.php, don't forget the trailing slash!

别忘了在 application/config/autoload.php

中加载 url 助手
$autoload['helper'] = array('url', 'assets');

在 .htaccess 中使用此代码

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/ [L]

你的 htaccess 应该是这样的,

RewriteEngine On
RewriteBase /finalProjectWork/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]