在CodeIgniter中动态加载视图

时间:2012-09-12 21:57:14

标签: php codeigniter

我是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 **

有人可以告诉我,如果我遗失了某些东西,或者这是不是很明显。

1 个答案:

答案 0 :(得分: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;