2009-07-23 66 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> 

我从来没有见过一个理由来分解出页眉和页脚视图。

相关问题