首页 > 开发 > PHP > 正文

MVC with PHP(二)

2024-05-04 22:53:41
字体:
来源:转载
供稿:网友
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: [email protected]
* @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: [email protected]
* @version $id$
* @copyright 2004-10-26
**/

include_once ("./comm/smarty/smarty.class.php");
include_once ("./comm/config.inc.php");

abstract class controller
{
private $smarty;

/**
* 系统构建函数
* 初始化smarty
**/
function __construct()
{
$this ->smarty = new smarty();

$this->smarty->template_dir = "./view/templates";
$this->smarty->compile_dir = "./view/templates_c";
$this->smarty->cache_dir = "./view/cache";
$this->smarty->cache_lifetime = 60 * 60 * 24;
$this->smarty->caching = false;
$this->smarty->left_delimiter = "<{";
$this->smarty->right_delimiter = "}>";
}

/**
* 设置smarty模板路径
*
* @param string $template
**/
public function setsmartytemplate_dir($template)
{
$this->smarty->template_dir = $template;
}

/**
* 设置smarty是否进行缓存
*
* @param boolean $cache
**/
public function setsmartycache($cache = false)
{
$this->smarty->cache = $cache;
}

/**
* 设置smarty缓存时间
*
* @param string $cachelifetime
**/
public function setsmartycachetime($cachelifetime)
{
$this->smarty->cache_lifetime = $cachelifetime;
}

/**
* 动作被执行后一个短暂的提示
*
* @param string $module 重新定向到的模块名称
* @param string $action 重新定向的动作名称
* @param string $params 参数名称
* @param string $message 提示信息
**/
public function redirect($module, $action, $params="", $message="动作已经被成功执

行")
{
$time = wait_for_time;
$params = ("" == $params) ? "" : "&$params";
$url = "?module=" . $module . "&action=" . $action . $params;

//重新定smarty模板目录至公用目录
$this->setsmartytemplate_dir("./view/templates");
$this->smarty->assign("url", $url); //重定向的目录
$this->smarty->assign("message", $message); //提示信息
$this->smarty->assign("time", $time);
$this->smarty->display("wait.html");
}

/**
* 调用本类不存在的方法时进行的处理
*
* @param string $name
* @param string $parameter
**/
public function __call($name, $parameter)
{
throw new actionnotallowexception("对不起,你所请求的动作 <b>$name</b> 没有定义

...<br>");
}

/**
* 析构函数
*
**/
public function __destruct()
{
unset($this->smarty);
}
}
?>

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

==============================================
controller是一个抽象类,也就是说它不可以直接使用new 来产生一个实例对象,在类的构造函数里产生一个smarty类,并对其进行基本的设置。其它的几个函数是对smarty对象进行设置的成员函数, 这里来看看这两个函数:

public function redirect($module, $action, $params="", $message="动作已经被成功执行"):
这是一个重新定向成员函数,它的作用是当我们对模块进行一些操作后给出的提示页面,然后经过设置好的时间自动重新定向到另一个位置,例如我们要对新闻进行一些删除,删除成功后我们要给用户返回这样一个页面,告诉用户操作已经成功,请待n秒后自动返回....这在论坛中是很常见的,这里我也引用了这样的策略。

public function __call($name, $parameter):
当类调用类没有声明的函数时使用这个函数进行处理,这可是个好东东,有了它,可以使用对程序

的控制更加简单了,大家可以试试这个方法....

好了,controller 部分就谈到这里了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表