简体   繁体   English

Codeigniter链接到样式表

[英]Codeigniter Link to Stylesheet

I am trying to learn Codeigniter. 我正在尝试学习Codeigniter。 I have built a simple MVC following the tutorials on the CI site. 我已经按照CI网站上的教程构建了一个简单的MVC。 This is my controller... 这是我的控制器...

<?php

class Pages extends CI_Controller {

    public function view($page = 'home'){

        if(!file_exists(APPPATH.'/views/pages/'.$page.'.php')){
            show_404();
        }

        $data['title'] = ucfirst($page);

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}

Here is the header template... 这是标题模板...

<html>
<head>
    <title><?php echo $title ?> - WurkWithUs</title>
    <link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/bootstrap.css" type="text/css" media="screen"/>
    <link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/custom.css" type="text/css" media="screen"/>
</head>
<body>
    <h1>Wurk With Us</h1>

The style sheets work when I use an absolute path like the one above. 当我使用上述绝对路径时,样式表将起作用。 However if I try to use something like... 但是,如果我尝试使用类似...

<link rel="stylesheet" href="<?= base_url()?>css/bootstrap.css" type="text/css" media="screen"/>

I get a blank page. 我得到空白页。 When I "view page source" my style line looks like... 当我“查看页面源代码”时,我的样式行看起来像...

    <link rel="stylesheet" href="

I have tried to follow the suggestions here and I get the same results ie document stops being parsed at the "href=" line. 我尝试遵循此处的建议,但得到的结果相同,即文档停止在“ href =”行进行解析。

What am I doing wrong? 我究竟做错了什么? Is there a better way to load my stylesheets? 有没有更好的方式来加载样式表?

I've had this issue many, many times. 我有很多次这个问题。 I'd bet money you're not loading in the URL helper . 我敢打赌,您没有在URL helper中加载钱。 It does not autoload, so either load it in your controller or autoload it. 它不会自动加载,因此可以将其加载到控制器中或自动加载。

If you choose to lead it in your controller, call 如果您选择将其引导到控制器中,请致电

$this->load->helper('url');

before you load the view. 在加载视图之前。

The reason you see the href blank in the source is because there was a PHP error at that point (it could not find the method base_url() ) 您在源代码中看到href空白的原因是因为此时存在一个PHP错误(找不到方法base_url()

Try this: 尝试这个:

<?php base_url("css/bootstrap.css"); ?>

Thanks 谢谢

Try this in your controller 在您的控制器中尝试

$this->load->helper('html');

and this in the head tag 这在头标签中

<?=link_tag('css/style.css')?>

I think you need to load the "url" helper. 我认为您需要加载“ URL”助手。 Load your helper in autoload.php 在autoload.php中加载您的助手

Like that way :: $autoload['helper'] = array('url');

This will auto load your helper every time. 每次都会自动加载您的助手。 So, you need not to load it again and again in every controller. 因此,您无需一次又一次地在每个控制器中加载它。

or 要么

If You want to load this helper for specific controller, then load the helper in your controller call 如果要为特定控制器加载此帮助程序,则在控制器调用中加载该帮助程序

Like that way :: $this->load->helper('url');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM