日期:2014-04-08  浏览次数:20342 次

MVC with PHP(一)中的bug的问题是存在,最大的问题是日志系统的问题,等完成这这个介绍后我后把全部更正的程序源码打包
出来,这里就暂时不做更改了.
先来看看在application.class.php中是如何建立controller实例的:

PHP代码:--------------------------------------------------------------------------------
/**
* 执行函数
*
* 此类唯一对外的一个接口
**/
public function run()
{
$this->parsePath();
$this->checkSecurity($this->module, $this->action);
1. $controller = new $this->controllerClassName();
2. $controller->{$this->action}();
$this->writeLog($this->module, $this->action);
}

--------------------------------------------------------------------------------

Application这个类在实例后唯一可进行调用的一个函数,它根据用户的URL请求来分析得出所需要的Controller类名,然后实例化这个类(上面标1的地方),再调用从URL中获取的动作名称(上面标2的地方),

这个举一个简单的例子:
URL: http://localhost/?module=news&action=showList
Application通过分析这个URL重到controllerClassName=news, action=showList,然后它将在包含处理这个controller类的文件名(在Application->getControllerFile()中进行),然后实例化News这个
controller类(标1的地方), 随后调用它的动作showList(标2的地方).
来看看newsController.php中的内容:
=============================================================

PHP代码:--------------------------------------------------------------------------------

<?php
/**
* FileName: newsController.php
* Introduce: 新闻控制类
*
* @author: 大师兄
* @Email: teacherli@163.com
* @version $Id$
* @copyright 2004-10-26
**/
include_once ("./controller/comm/controller.class.php");
include_once ("./model/news/newsModel.php");

class NewsController extends Controller
{
private $model;

/**
* 构造函数
*
**/
public function __construct()
{
parent::__construct();
$this->model = new NewsModel();
$this->setSmartyTemplate_dir("./view/news");
}

/**
* 显示新闻列表
*
**/
public function showList()
{
1. $newsList = & $this->model->getList();

2. $this->smarty->assign("newsList", $newsList);
3. unset($newsList);

4. $this->smarty->display("newsList.html");
}
}
?>

--------------------------------------------------------------------------------

==============================================================
首先,NewsController类继承自公共类Controller,在类进行初始化时产生一个NewsModel类,这个类是一个model类,由这个类负责新闻模块所有的对数据库的交互. parent::__construct()调用父类的构造函数,完成对view的控制类Smarty的初始化.$this->setSmartyTemplate_dir("./view/news")将模板目录定位在./view/news目录.

然后看我们上面的例子,请求URL为http://localhost/?module=news&actio...List,表示要调用
showList这个动作,看NewsController类的showList()成员函数:
1. $newsList = & $this->model->getList(): $this->model在NewsController初始化时建立,这一句要使用$this->model从数据库里提取出一个新闻列表,这个列表当然就是Smarty在操作循环块时需要的二维数组了,在NewsModel类中,它是采用ADODB回传的一个二维数组.
2. $this->smarty->assign("newsList", $newsList): 熟悉吧,smarty中循环块的程序控制
3. unset($newsList):考虑到效率问题,对于这些临时变量在使用完成后即时将它unset。
4. $this->smarty->display("newsList.html"):使用smarty来显示view.

大家看明白了吗?实际上controller类要做的事情就是这样:1.调用model从数据库取出记录 2.操

作Smarty显示view。
再来看看NewsController的父类Controller类的源码:
===========================================================

PHP代码:--------------------------------------------------------------------------------

<?php
/**
* FileName: controller.class.php
* Introduce: Base class of controller
*
* @author: 李晓军
* @Email: teacherli@163.com
* @version