php 如何在codeigniter中加载css
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11824555/
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
How to load css in codeigniter
提问by Soundhar Raj
I am new to codeigniter, and I am using v.2.12. I am getting an error when I try to load the css from the external file.
我是 codeigniter 的新手,我使用的是 v.2.12。当我尝试从外部文件加载 css 时出现错误。
I create the css folder inside the application folder. And I create the css file in the name of all.css.
我在应用程序文件夹中创建了 css 文件夹。我以all.css 的名称创建了css 文件。
In the view file I use the following code to link the css file.
在视图文件中,我使用以下代码链接 css 文件。
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>css/all.css">
But the css file is not loading. I'm getting 404 error. Here is my configuration settings:
但是 css 文件没有加载。我收到 404 错误。这是我的配置设置:
$config['base_url'] = 'http://webscarlets.com/ci/index.php';
$config['index_page'] = 'index.php';
Website Link: http://webscarlets.com/ci/index.php/welcome.
采纳答案by Alaa Badran
The function base_url()should return the base path (without index.php)
函数base_url()应该返回基本路径(没有 index.php)
You may fix it by adding a backslash like:
您可以通过添加反斜杠来修复它,例如:
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>/css/all.css">
or remove the index.php from your config:
或从您的配置中删除 index.php:
$config['base_url'] = 'http://webscarlets.com/ci/';
回答by Christian Davén
This is how you include CSS files in CodeIgniter:
这是在 CodeIgniter 中包含 CSS 文件的方式:
<?php echo link_tag('css/mystyles.css'); ?>
That snippet will output this HTML:
该片段将输出此 HTML:
<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
The function link_tagis in the HTML helper, which must first be loaded.
该函数link_tag位于 HTML helper 中,必须首先加载它。
(Note that you probably shouldn't put your CSS files in /application/css. It's just easier to put them in /cssor maybe /assets/css.)
(请注意,你可能不应该把你的CSS文件中/application/css,把它们放在这只是更容易/css或可能/assets/css)。
回答by Jorge Luis Jiménez Prada
I just find the solution to avoid index.php file and load ours CSS files. Just copy the code below in a .htaccess file:
我只是找到了避免 index.php 文件并加载我们的 CSS 文件的解决方案。只需将以下代码复制到 .htaccess 文件中:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|images|styles|scripts|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L,QSA]
Greeting!
问候!
回答by Gampesh
Add below line in your controller action /application/controllers/yourController.php
在您的控制器操作 /application/controllers/yourController.php 中添加以下行
$this->load->helper('url');
Then add below line to your view file's head tag.
然后将下面的行添加到您的视图文件的 head 标记中。
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/yourcssfile.css');?>" />
Assuming that you have assets/css/ folder is created in your app directory.
假设您在应用程序目录中创建了 assets/css/ 文件夹。
<path to your app folder>/assets/css
回答by Saptarshi Chaudhuri
Edit autoload.php as follows
编辑 autoload.php 如下
$autoload['helper'] = array('url');
Then load css , js,image like that
然后像这样加载 css , js, image
img src="<?php echo base_url(); ?>assets/images/master.jpg"</img>
回答by SMHasnain
To attach a CSS, JS, Images .etc you just have to do is go to your configfolder and write at the end of the constant.phpfile.
要附加 CSS、JS、Images 等,您只需转到config文件夹并在constant.php文件的末尾写入。
define('URL','ADD YOUR LOCAL/REMOTE PATH');
define('CSS',URL.'public/css/');
define('IMAGES',URL.'public/images/');
define('JS',URL.'public/images/');
After that goto your view and in the link just add
之后转到您的视图并在链接中添加
<link rel="stylesheet" type="text/css" href="<?php echo CSS; ?>index.css">
this will solve your problem.
这将解决您的问题。
Hope it helps.
希望能帮助到你。
回答by jogesh_pi
before using the base_url()you should have to load the URL helper class.
在使用之前,base_url()您应该加载 URL 帮助程序类。
something like $this->load->helper('url');in your controller
类似于$this->load->helper('url');你的控制器
base_url()return you the path something like'http://webscarlets.com/'
if you have set it directly in the root or 'http://webscarlets.com/dir/'
base_url()将路径返回给您,就像'http://webscarlets.com/'
您直接在根目录中设置它一样,或者'http://webscarlets.com/dir/'
and also make sure about the location of your CSS file.
并确保您的 CSS 文件的位置。
follow the link to know more about URL Helper
按照链接了解有关URL Helper 的更多信息
回答by WatsMyName
another way would be
另一种方式是
define a constant in constants.php (in config directory)
在constants.php 中定义一个常量(在config 目录中)
define("LAYOUT_URL","http://localhost/yoursite/css/");
"css" folder here i m assuming is inside application folder. NOw you can attach css in page like
我假设这里的“css”文件夹位于应用程序文件夹内。现在您可以在页面中附加 css,例如
<link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URL;?>all.css">
回答by tinhphaistc
As Jogesh_p.
作为Jogesh_p。
you use base_urlas follow put in controller (your controller)
您使用base_url如下放入控制器(您的控制器)
$this->load->helper('url');
in controller. if you want to use
在控制器中。如果你想使用
as follow put in where you want use base_url.
如下放置在您想要使用 base_url 的位置。
echo base_url()
NOTE: better you create new folder at root
注意:最好在根目录下创建新文件夹
(Example: theme) same: application, system, user_guide, theme)
(例如:主题)相同:应用程序、系统、用户指南、主题)
i hope can you do
我希望你能做到
回答by mrsrinivas
//config.php
$config['base_url'] = 'http://webscarlets.com/ci/';
$config['index_page'] = 'index.php';
and try to load cssby adding application folder
并尝试css通过添加应用程序文件夹来加载
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>application /css/all.css">
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>application /css/all.css">
EDIT
编辑
Here
base_url()echos 'http://webscarlets.com/ci/'then adding the file with path application /css/all.css
这里
base_url()回显然'http://webscarlets.com/ci/'后添加带有路径的文件application /css/all.css

