笨 - 加载CSS(CodeIgniter - Loading CSS)

2019-06-24 11:12发布

我开发一个Web解决方案的定制API,我使用MVC设计模式。 我有一个模块的文件夹,这样我可以换入或换出的模块,也无需中断工作的测试代码的部分工作。 我唯一的问题是现在,我想在任何地方加载CSS,并有我的应用程序正确导入CSS文件头标记。 我知道笨做到这一点,但我不知道怎么样。

使用PHP,我怎么在CSS文件中加载的任何地方,然后有代码的头部标签,如笨不中正确导入的CSS?

提前致谢。

Answer 1:

您可以加载在一次其他视图里面的几个观点或意见。

因此,在这种情况下,我建议你在哪里,你都加载CSS和JS文件创建一个头视图

例:

<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>

而这样称呼它:

$this->load->view('header');
$this->load->view('view1');
$this->load->view('view2');

这样,您就可以控制文件(CSS + JS +等),你加载只有一个文件。

regrads,
佩德罗
@pcamacho



Answer 2:

你的问题是有点我不清楚,但我会尽我所能来帮助。 你只是想知道如何在您的浏览CSS文件? 如果是这样,只需使用以下命令:

<style> @import url('/css/styles.css'); </style>

如果你的CSS文件夹是在您的CodeIgniter项目的根,你可以用CodeIgniter的BASE_URL()函数做这样的事情:

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

这将确保您的页面停留便携,并有正确的绝对URL。 希望这可以帮助! 如果没有,尝试成为一个更具体一点在你的问题



Answer 3:

使用绝对...

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

...或相对根URI。

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


Answer 4:

还请确保您没有在.htaccess文件重定向的URL,让CSS

.htaccess文件

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

而且,这里就是我在我的头模板添加

$this->load->helper("url");
echo link_tag(base_url().'css/styles.css');


Answer 5:

你可以这样做,因为我做的,在你的视图文件执行下列操作

<link rel="stylesheet" href="{pathToApp}/styles/main.css" type="text/css" media="screen" />

在您的控制器,然后(在创建应用程序目录中的文件夹的样式后),通过这个:

$path = '../system/application';

到一个数组并发送数组作为第二个参数,如果你正在使用负载>查看加载您的视图的,你需要{pathToApp}更改为$阵列[“路径”(改变$阵列,无论你叫它)。 如果你正在使用CodeIgniter的内置的模板系统,那么你都设置。 至少用这种方式,你将不再需要在迁移你的网站改变绝对URL。



Answer 6:

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 jack 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.



Answer 7:

首先创建一个控制器的功能和加载在笨调用CSS的HTML帮手。

示例代码

<?php 
class Home extends CI_Controller{

    public function helper(){
        $this->load->helper('html');    
        $this->load->helper('url'); 
        $this->load->view('index');

    }
}
?>

然后,使用调用CSS文件link_tag关键词。 像这样的事情在index.php文件

<html>
<head>
    <title></title>
     <?php echo link_tag('css/moorainbow.css');?>
</head>
<body>
<?php 
.....
?>
</body>
</html>

这里的css/是包含moorainbow.css文件的文件夹。



Answer 8:

做这个

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

但先在根目录下的css文件夹,并把那里的style.css。



文章来源: CodeIgniter - Loading CSS