2012-09-12 164 views
1

我是CI新手,希望獲得一些反饋給我的代碼,只是想知道我的思維過程在哪裏,如果我真的讓我的腦袋纏繞CI。在CodeIgniter中動態加載視圖

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

我也有一個叫家在控制器的目錄,此代碼控制器:

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

**最後,我有一個名爲默認的視圖,它保存我的頁眉和頁腳信息。現在我想動態加載正確的內容視圖,取決於我在我的網站中的位置。這就是我對我的默認視圖:

<!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的輸出壓縮' 文件名衝突:芯/ Output.php 行號:379 **

** A PHP錯誤遇到

嚴重性:注意 消息:ob_start():未能創建緩衝器 文件名:芯/ Output.php 行號:379 **

有人可以告訴我,如果我失去了一些東西,或者如果這只是明顯不正確。

+0

我做了類似的事情 - 所以你的做法是正確的。但是你的錯誤信息看起來不尋常去掉你的視圖和控制器,看看你是否可以隔離是什麼導致它 – Laurence

+0

你的代碼是正確的..但這個錯誤是不尋常的.....你正在使用的CI版本? – chhameed

回答

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;