dongshanfan1941 2012-09-12 21:57
浏览 49
已采纳

在CodeIgniter中动态加载视图

I am new to CI and wanted to get some feed back on my code and just get a feel of where my thought process is and if I am really getting my head wrapped around CI.

I have a file in my core directory called My_Controller which has this code:

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

I also have a controller called home in the controller directory with this code:

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

**And finally I have a view named default which hold my header and footer info. Now I would like to dynamically load the correct content view depending on where I am in my site. This is what I have for my default view:

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

I get the correct syntax from home.php but I also get these errors:

** A PHP Error was encountered

Severity: Warning Message: ob_start(): output handler 'ob_gzhandler' conflicts with 'zlib output compression' Filename: core/Output.php Line Number: 379**

** A PHP Error was encountered

Severity: Notice Message: ob_start(): failed to create buffer Filename: core/Output.php Line Number: 379**

Can someone please tell me if I'm missing something or if this is just plain incorrect.

  • 写回答

1条回答 默认 最新

  • douxi3432 2012-09-13 05:34
    关注

    My guess is that you have Output Compression enabled and you are prematurely echoing something to the browser. Head into config/config.phpand disable output compression:

    /*
    |--------------------------------------------------------------------------
    | 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料