php CodeIgniter - 加载 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1060733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter - Loading CSS
提问by Torez
I am developing an custom API for a web solution and I am using the MVC design pattern. I have a modules folder so that I can swap in and out modules and also work on sections without disrupting working tested code. My only issue now is that I want to load CSS anywhere and have my application properly import the css file in the head tag. I know CodeIgniter does this but I'm not sure how.
我正在为 Web 解决方案开发自定义 API,并且我正在使用 MVC 设计模式。我有一个模块文件夹,这样我就可以换入和换出模块,还可以在不中断工作的测试代码的情况下处理部分。我现在唯一的问题是我想在任何地方加载 CSS 并让我的应用程序在 head 标记中正确导入 css 文件。我知道 CodeIgniter 这样做,但我不确定如何。
Using PHP, how do I load in a CSS file anywhere and then have the code properly import the css within the head tags like CodeIgniter does?
使用 PHP,我如何在任何地方加载 CSS 文件,然后让代码像 CodeIgniter 一样在 head 标签中正确导入 css?
Thanks in advance.
提前致谢。
回答by Pedro
You can load several views at once, or views inside other views.
您可以一次加载多个视图,或其他视图中的视图。
So in this case I recomend you to create one header view where you load all css and js files
所以在这种情况下,我建议您创建一个标题视图,您可以在其中加载所有 css 和 js 文件
example:
例子:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<link rel="stylesheet" href="<?php echo base_url();?>css/moorainbow.css" type="text/css" media="screen"/>
</head>
<body>
And call it like:
并称之为:
$this->load->view('header');
$this->load->view('view1');
$this->load->view('view2');
This way you can control the files (css+js+etc) you load in just one file.
这样你就可以控制你在一个文件中加载的文件(css+js+etc)。
Regrads,
Pedro
@pcamacho
Regrads,
佩德罗
@pcamacho
回答by Colin Brock
Your question is a little unclear to me, but I'll do my best to help. Are you simply wondering how to include a CSS file in your Views? If so, simply use the following:
你的问题对我来说有点不清楚,但我会尽力提供帮助。您是否只是想知道如何在您的视图中包含 CSS 文件?如果是这样,只需使用以下内容:
<style> @import url('/css/styles.css'); </style>
If your CSS folder is at the root of your CodeIgniter project, you could do something like this using CodeIgniter's base_url() function:
如果您的 CSS 文件夹位于您的 CodeIgniter 项目的根目录,您可以使用 CodeIgniter 的 base_url() 函数执行以下操作:
<style> @import url('<?=base_url()?>/css/styles.css'); </style>
It will ensure your pages stay portable and have the correct absolute URL. Hope this helps! If not, try being a little more specific in your question
它将确保您的页面保持可移植性并具有正确的绝对 URL。希望这可以帮助!如果没有,请尝试在您的问题中更具体一点
回答by Quentin
Use an absolute …
使用绝对...
<link rel="stylesheet" type="text/css" href="http://example.com/style/css">
… or root relative URI.
... 或根相对 URI。
<link rel="stylesheet" type="text/css" href="/style/css">
回答by mc.
also make sure you aren't redirecting the url in the .htaccess file, allow css
还要确保您没有重定向 .htaccess 文件中的 url,允许 css
.htaccess file
.htaccess 文件
RewriteEngine on
RewriteCond !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ index.php/ [L]
Also here's what i added in my header template
这也是我在标题模板中添加的内容
$this->load->helper("url");
echo link_tag(base_url().'css/styles.css');
回答by Marc Towler
you can do the same as i do, in your view file do the following
您可以像我一样做,在您的视图文件中执行以下操作
<link rel="stylesheet" href="{pathToApp}/styles/main.css" type="text/css" media="screen" />
(after creating a styles folder in the application directory) then in your controller, pass this:
(在应用程序目录中创建一个样式文件夹之后)然后在你的控制器中,传递这个:
$path = '../system/application';
into an array and send the array as the second parameter, if you are using load->view to load your view's you need to change {pathToApp} to $array['path'] (change $array to whatever you called it). If you are using CodeIgniter's built in templating system then you are all set. At least with this way you won't need to change the absolute URL when you migrate your site.
放入数组并将数组作为第二个参数发送,如果您使用 load->view 加载视图,则需要将 {pathToApp} 更改为 $array['path'] (将 $array 更改为您调用的任何内容)。如果您使用的是 CodeIgniter 的内置模板系统,那么您就大功告成了。至少通过这种方式,您在迁移站点时不需要更改绝对 URL。
回答by Dane Calderon
One important thing that I realized is that the RewriteCond in the .htaccess refers to FOLDERS not FILE TYPES. In other words, putting |css|in there doesn't do Hyman unless your stylesheets are in the /css/ folder. List the actual folders, so |styles|or |scripts|or whatever. THEN and only then will using "<?=base_url()?>styles/style.css"(for example) show up. It took me about 2 days to figure this out, so I hope reading this saves someone some hair and high blood pressure.
我意识到的一件重要事情是 .htaccess 中的 RewriteCond 指的是文件夹而不是文件类型。换句话说,|css|除非您的样式表在 /css/ 文件夹中,否则放入那里不会产生 Hyman。列出实际文件夹,因此|styles|或|scripts|或什么的。THEN,然后才会使用"<?=base_url()?>styles/style.css"(例如)出现。我花了大约 2 天的时间才弄明白这一点,所以我希望阅读这篇文章可以为某人节省一些头发和高血压。
回答by NIMISHAN
First you create a Controller function and load the HTML helper for call css in Codeigniter.
首先,您创建一个控制器函数并加载用于在 Codeigniter 中调用 css 的 HTML 帮助程序。
sample code
示例代码
<?php
class Home extends CI_Controller{
public function helper(){
$this->load->helper('html');
$this->load->helper('url');
$this->load->view('index');
}
}
?>
Then to call the css file using link_tagkey word. Something like this in the index.phpfile in
然后使用link_tag关键字调用css文件。index.php文件中的 类似内容
<html>
<head>
<title></title>
<?php echo link_tag('css/moorainbow.css');?>
</head>
<body>
<?php
.....
?>
</body>
</html>
Here the css/is the folder that contain the moorainbow.css file.
这css/是包含 moorainbow.css 文件的文件夹。
回答by surafel
do this
做这个
<link rel="stylesheet" type="text/css" href="./css/style.css">
<link rel="stylesheet" type="text/css" href="../css/style.css">
but first create a css folder in the root directory and put style.css there.
但首先在根目录中创建一个 css 文件夹并将 style.css 放在那里。

