1

我是 CI 的新手,想获得一些关于我的代码的反馈,并了解我的思考过程在哪里,以及我是否真的对 CI 有所了解。

我的核心目录中有一个名为 My_Controller 的文件,其中包含以下代码:

<?php

class MY_Controller extends CI_Controller
{
public function __construct()
{
    parent::__construct();

    $this->data = array(
        'title' => '',
        'keywords' => 'default keywords',
        'description' => 'default description',
        'layout' => 'default',
        'view' => '',
        'body_class' => '',
        'data' => array()
    );
}
}

我在控制器目录中还有一个名为 home 的控制器,代码如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller
{
function __construct()
{
    parent::__construct();
}
public function index()
{
    /*
    Set variables to send to the wrapper template
    Default values are stored in /application/core/MY_Controller.php
    */

    $this->data['title'] = 'Home';
    $this->data['keywords'] = 'Home';
    $this->data['description'] = 'Home';
    $this->data['view'] = 'content/home';
    $this->data['body_class'] = 'home';
    $this->data['data'] = array(); // associative array of values you can pass to the view

    $this->load->view('layout/default', $this->data);
}
}

**最后我有一个名为 default 的视图,其中包含我的页眉和页脚信息。现在我想根据我在站点中的位置动态加载正确的内容视图。这是我的默认视图:

<!doctype html>
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title></title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="keywords" content="">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">

<link rel="stylesheet" href="/assets/css/reset.css">
<link rel="stylesheet" href="/assets/css/app.common.css">

<script src="/assets/js/libs/modernizr-2.0.6.min.js"></script>

</head>

<body class="<?php echo $body_class; ?>">
<div id="wrapper">
    <div id="container">
        <header id="header">
            <nav>
                <ul>
                    <li>
                        <a href="/">Home</a>
                    </li>
                </ul>
            </nav>
        </header>
        <section id="main" role="main">
            <?php echo $this->load->view($view); ?>
        </section>
        <footer id="footer"></footer>
    </div> 
</div>
<!--jQuery Libraries and Plugins-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/libs/jquery-1.8.0.min.js"><\/script>')</script>

<!--Other JavaScript Libraries and Plugins-->

<script type="text/javascript"> // Change UA-XXXXX-X to be your site's ID
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

<!--[if lt IE 9]> 
    <script src="/js/libs/selectivizr-1.0.2.min.js"></script>
<![endif]-->        

<!--Application JavaScript-->

<script src="js/app/app.common.js"></script>

</body>
</html>

我从 home.php 中得到了正确的语法,但我也得到了这些错误:

** 遇到 PHP 错误

严重性:警告消息:ob_start():输出处理程序“ob_gzhandler”与“zlib 输出压缩”冲突文件名:core/Output.php 行号:379**

** 遇到 PHP 错误

严重性:通知消息:ob_start():创建缓冲区失败文件名:core/Output.php 行号:379**

有人可以告诉我我是否遗漏了什么,或者这是否完全不正确。

4

1 回答 1

0

我的猜测是您启用了输出压缩,并且您过早地向浏览器回显某些内容。进入config/config.php并禁用输出压缩:

/*
|--------------------------------------------------------------------------
| Output Compression
|--------------------------------------------------------------------------
|
| Enables Gzip output compression for faster page loads.  When enabled,
| the output class will test whether your server supports Gzip.
| Even if it does, however, not all browsers support compression
| so enable only if you are reasonably sure your visitors can handle it.
|
| VERY IMPORTANT:  If you are getting a blank page when compression is enabled it
| means you are prematurely outputting something to your browser. It could
| even be a line of whitespace at the end of one of your scripts.  For
| compression to work, nothing can be sent before the output buffer is called
| by the output class.  Do not 'echo' any values with compression enabled.
|
*/
$config['compress_output'] = FALSE;
于 2012-09-13T05:34:49.253 回答