2009-07-23 23 views
2

CodeIgniter中使用控制器/視圖的最佳方式是什麼?這是在CodeIgniter中使用控制器和視圖的好方法嗎?

以下是關於製作我的網頁的不好方法嗎?

function index() 
{ 
    $this->load->library('carabiner');  
    $this->load->view('include/head'); 
    $this->load->view('include/home'); 
    $this->load->view('top'); 
    $this->load->view('featured'); 
    $this->load->view('eventslist'); 
    $this->load->view('footer');  
} 

function create() 
{ 
    $this->load->view('include/head'); 
    $this->load->view('top'); 
    $this->load->view('page'); 
    $this->load->view('dynamicstuff'); 
    $this->load->view('footer'); 
} 

我準模板化這種方式,它工作正常用於獲取網站的輪廓,但它創建一個應用程序的最佳方式? 我一直在CodeIgniter工作大約一週,而且我是PHP新手。

回答

1

有一個名爲CodeIgniter Library下載,使得這一切很容易。一旦你學會了如何實現它,編碼變得非常簡單。它還允許您更改每個頁面或控制器以具有不同的模板(佈局)。

使用我上面你提到的系統可以做到以下幾點:

<?php 

    function index() { 

     $data['some_variable'] = 'some data'; 

     $this->template->write_view('content', 'page/home', $data); 
     $this->template->render(); 

    } 

?> 

任何地方,你也可以更改模板要使用:

<?php 

    $this->template->set_template('login'); 

?> 

所有這些模板有保存名稱在配置文件中。您可以擁有任意數量的模板。

模板文件本身會是這個樣子:

<html> 
.... etc. all other html elements 
    <body> 
     header html goes here 

     <?=$content?> 

     footer html goes here 
    </body> 
</html> 

,你甚至可以設置部分,以能夠寫入您的內容。我通常不這樣做,因爲它包括了很多意見,但如果你需要這樣的完全控制,你仍然可以這樣做:

<?php 

    function index() { 

     $data['header'] = 'header info'; 
     $data['content'] = 'content info'; 
     $data['sidebar'] = 'sidebar info'; 
     $data['footer'] = 'footer info'; 

     $this->template->write_view('content', 'page/home', $data); 
     $this->template->write_view('header', 'modules/header'); 
     $this->template->write_view('sidebar', 'modules/sidebar'); 
     $this->template->write_view('footer', 'modules/footer'); 

     $this->template->render(); 

    } 

?> 

HTML模板代碼:

<html> 
.... etc. all other html elements 
    <body> 
     <div id="header"> 
      <?=$header?> 
     </div> 

     <div id="content"> 
      <div id="left"> 
       <?=$content?> 
      </div> 

      <div id="sidebar"> 
       <?=$sidebar?> 
      </div> 
     </div> 

     <div id="footer"> 
      <?=$footer?> 
     </div> 
    </body> 
</html> 

這樣,您只需要擔心在所有控制器功能中包含一個文件。如果您不使用PHP 5(您應該切換),那麼您將不想使用<?=$content?>,而是使用<?php echo $content; ?>

3

我將加載每個函數只有一個視圖:

function index() 
{ 
     $this->load->library('carabiner');    
     $this->load->view('index'); 
} 

function create() 
{ 
     $this->load->view('create'); 
} 

然後包括其它模板文件例如爲的index.php:

<?php 
    include 'include/head'; 
    include 'include/home'; 
    include 'top'; 
    include 'featured'; 
    include 'eventslist'; 
    include 'footer'; 
?> 

當然,你可以組這些觀點本身,然後包括一個觀點本身包括頭和導航等

+0

這會提高性能嗎? – 2009-07-24 01:02:48

+0

網站的性能?我不這麼認爲。編程的性能?當然,因爲您將在佈局和內容之間分離控制器和視圖,並且正確執行。 – Residuum 2009-07-24 08:11:25

3

在我所有的控制器中,我都有一個數組,我稱之爲$ page_data。這包含了我需要傳遞給我的視圖的所有內容:頁面標題,要加載的資源(js/css),元描述和內容。

它看起來是這樣的:

function index(){ 
    $page_data['title'  ] = 'My Page Title'; 
    $page_data['description' ] = 'All about my great page.'; 
    $page_data['content_file' ] = 'pages/index'; 
    $page_data['content_data']['books'] = $this->Books_model->get_books(); 
    $this->load->view('page-template', $page_data); 
} 

然後根據意見,我有 '頁面的template.php', '的header.php', 'nav.php', 'footer.php' 和'頁/ index.php文件」。所有這些都是基本的HTML/PHP和頁面模板。PHP看起來像這樣:

... 
<title><?php echo $title ; ?></title> 
.... 
<body> 
    <?php $this->load->view('header'); ?> 
    <?php $this->load->view('nav'); ?> 
    <?php $this->load->view($content_file, $content_data); ?> 
    <?php $this->load->view('footer'); ?> 
</body> 

按照同樣的模式,你會發現它很方便有一個「局部的template.php」也許「JSON-的template.php」爲Ajax /數據飼料頁。

隨着網站的發展,這種模式非常靈活。

0

我的解決方案。

function index() 
{ 
    $data['main'] = 'content_view'; 

    $this->load->vars($data); 
    $this->load->view('template'); 
} 

則...的template.php

<html> 
<body> 
<div class="header">.... blah...</div> 
<div class="main"><?php $this->load->view($main); ?></div> 
<div class="footer">.... (c)2009, etc.</div> 

我從來沒有見過一個理由來分解出頁眉和頁腳視圖。

相關問題