【问题标题】:Codeigniter Link to StylesheetCodeigniter 链接到样式表
【发布时间】:2012-09-15 00:49:23
【问题描述】:

我正在尝试学习 Codeigniter。我按照 CI 网站上的教程构建了一个简单的 MVC。这是我的控制器...

<?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);
    }
}

这是标题模板...

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

当我使用像上面这样的绝对路径时,样式表会起作用。但是,如果我尝试使用类似...

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

我得到一个空白页。当我“查看页面源代码”时,我的样式线看起来像......

    <link rel="stylesheet" href="

我尝试遵循here 的建议并得到相同的结果,即文档停止在“href=”行进行解析。

我做错了什么?有没有更好的方法来加载我的样式表?

【问题讨论】:

    标签: php css codeigniter


    【解决方案1】:

    我遇到过这个问题很多很多次了。我敢打赌你不会在URL helper 中加载钱。它不会自动加载,所以要么将它加载到你的控制器中,要么自动加载它。

    如果您选择在控制器中引导它,请调用

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

    在加载视图之前。

    您在源代码中看到 href 空白的原因是因为此时出现 PHP 错误(找不到方法 base_url()

    【讨论】:

    • 是的,只是想通了,感谢您的帮助。我只是在写我自己的答案。阿罗哈
    【解决方案2】:

    试试这个:

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

    谢谢

    【讨论】:

    • 那不行。无论如何,那些双引号在 href 中不起作用。我尝试了这个建议的每一个变体,用不同的方式转义引号,得到了相同的结果。
    • @shakabra - 这不是您问题的解决方案,但它是使用 base_url 函数的正确方法。引号在那里是因为您将字符串传递给函数。
    【解决方案3】:

    在你的控制器中试试这个

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

    这个在head标签中

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

    【讨论】:

      【解决方案4】:

      我认为您需要加载“url”帮助程序。 在 autoload.php 中加载你的助手

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

      这将每次自动加载您的助手。因此,您无需在每个控制器中一次又一次地加载它。

      如果您想为特定控制器加载此帮助程序,请在您的控制器调用中加载帮助程序

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

      【讨论】:

        猜你喜欢
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-13
        • 2015-11-12
        • 1970-01-01
        相关资源
        最近更新 更多