如何用Codeigniter构建一个动态网站


How to structure a dynamic website with Codeigniter

我有一些php的经验,但我最近才开始学习Codeigniter。我曾经有一个网站与固定的导航窗格和侧边栏,但网站的主要部分动态加载基于Get变量。基本上是

include head.php
include navbar.php
include sidebar.php
include the page requested from the get variable (home, about, contact, etc.)
include footer.php

我喜欢这个,因为当用户从一个页面导航到另一个页面时,整个网站不需要重新加载。

我不知道如何用Codeiginiter做到这一点。我应该为每个页面使用一个控制器还是为每个页面使用一个函数的控制器?有人知道有什么好的教程可以做类似的事情吗?我看过的所有教程都会为每个页面重新加载整个网站。

编辑:基本上我想这样做但是用Codeigniter

因为看起来你想要相对静态的内容,但动态加载,你可以用一个控制器和(也许)一个方法在控制器。

要用一种方法完成,请在welcome控制器中执行以下操作:

public page( $page_id ){
    // views/header.php
    $this->load->view( "header" );

    if( $page_id = "about" ){
      $this->load->view("about");     // views/about.php
    }
    else if( $page_id = "contact" ){
      $this->load->view("contact");  // views/contact.php
    }
    // views/footer.php
    $this->load->view("footer");
}

这需要一个get变量,并计算出在页眉和页脚之间加载哪个页面。

这样www.yoursite.com/page/about将加载about页面,www.yoursite.com/page/contact将加载contact页面

现在,如果你想摆脱/page部分,你需要做一些URL重路由在application/config/routes.php

或者你可以在一个控制器中使用几个方法:

public about(  ){
    // views/header.php
    $this->load->view( "header" );
    $this->load->view( "about" );
    // views/footer.php
    $this->load->view("footer");
}

public contact(  ){
    // views/header.php
    $this->load->view( "header" );
    $this->load->view( "contact" );
    // views/footer.php
    $this->load->view("footer");
}

现在你的url没有路由看起来更好,但是你必须为每个页面加载页眉/页脚

你真的喜欢复制/粘贴许多$this->load->view()到任何控制器函数吗?这是一个意大利面条代码。你可以尝试下一个:例如,我们有main.php controller作为默认控制器。本控制器主要功能如下:

public function index()
    {
        ob_start();
        $this->load->model('mainmodel');  
        $data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
        foreach ($data->result_array() as $row) {
            $this->load->module($row['block_name']);
            $this->name = new $row['block_name'];
            $this->name->index();
        }            
        ob_end_flush();
    } 

所以,每个控制器也有index()函数,可以根据url段调度动作,准备参数等

页脚控制器为例(我使用Smarty作为模板引擎):

public function index()
    { 
               $this->mysmarty->assign('year', date("Y"));
               $this->mysmarty->view('footer');
               return true;
    }

内容控制器将有:

public function index()
    {
        $name = $this->uri->segment(1, 'index');
        $act = $this->uri->segment(2, 'index');
        $this->load->module($name);
        $this->name = new $name;
        $pageData = $this->name->_show($act);
        if ($pageData)
        {
            $this->mysmarty->assign($name, $pageData);
        }
        $this->mysmarty->view($name);
    }

这意味着如果你想显示http://site.name/page/contactus,我们接下来要做什么:

2)首先显示header。TPL标头控制器

3)然后显示菜单

4)然后我们调用解析url的内容控制器,在页面控制器中找到他应该调用的_show()函数,并传递action='contactus'给它。_show()函数可以包含一些switch/case构造,这些构造显示模板依赖于动作名称(contactus)。在本例中为TPL)

5)最后显示页脚模板

在这种情况下,我们有灵活的结构。所有控制器都应该有index()函数,所有可以在content中调用的控制器都应该有_show($act)函数。这一切。

在codeIgniter中你可以这样做,你可以同时从你的控制器加载不同的视图。例如:

例如,在导航栏视图中,你的菜单中有一个Contacts按钮,它看起来像这样:

<a href='contacts'>Contacts</a>

在你的控制器中:

public function contacts()
{
    $this->load->view('header');
    $this->load->view('navbar');
    $this->load->view('sidebar');
    $this->load->view('contacts_view');
    $this->load->view('footer');
}

这里我们假设你已经有了下面的视图准备加载(header.php, navbar.php, sidebar.php, contacts_view.php, footer.php)

:你不需要$_GET[]请求,只需要在<a>锚标记

中提供控制器的方法名称

在codeigniter I使用模板

首先将模板文件与header.php, navbar.php等放在一个文件夹中

示例:template.php

<?php 
 echo $this->load->view('header'); //load header
 echo $this->load->view('navbar');//load navbar
 echo $this->load->view('sidebar');//load sidebar
 echo $this->load->view($body); //load dynamic content
 echo $this->load->view('footer');//load footer
?> 

second in controller

function index(  ){
$data['body'] = 'home'; // cal your content
$this->load->view('template', $data);
}