我正在使用带有codeigniter的php。我的css文件夹在像localhost/project/css/main.css这样的项目文件夹中,但我的view.php仍然无法访问该main.css它没有给我任何错误。但当我试图在控制台栏中显示incept元素时,它显示了css文件的错误404。这是在该位置可用时找不到追索权
以下是我喜欢如何处理资产。
我在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.php 中正确设置了base_url
$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/$1 [L]
试试这个
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/main.css');?>" />
您的htaccess应该像一样
RewriteEngine On
RewriteBase /finalProjectWork/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]