这是在CodeIgniter中使用控制器和视图的好方法吗?

时间:2009-07-23 21:19:27

标签: codeigniter templates controller

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的新手。

4 个答案:

答案 0 :(得分:3)

我只为每个函数加载一个视图:

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

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

然后包含其他模板文件,例如for index.php:

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

当然,您可以自己对这些视图进行分组,然后包含一个本身包含标题和导航等的视图。

答案 1 :(得分: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);
 }

然后在视图下,我有'page-template.php','header.php','nav.php','footer.php'和'pages / index.php'。所有都是基本的HTML / PHP,page-template.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>

遵循相同的模式,您可能会发现为ajax /数据Feed页面设置'partial-template.php'或'json-template.php'非常方便。

随着网站的增长,此模型非常灵活。

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

我从来没有看到过分析页眉和页脚视图的原因。