PHP的MVC框架开发简单示例
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码将业务逻辑和数据显示分离。
这里用一个简单的例子来演示MVC开发原理。
1.首先分别建立 模板目录(template)、动态页面目录(compile)、静态页面目录(cache),在template目录里建立模板文件 template.php
代码如下:
<html>
<head>
<title>{$title}</title>
</head>
<body>{$content}</body>
</html>
这里分别预制{$title} 和 {$content}2个变量。本次演示案例的功能,就是将模板变量替换成赋值后,生成最终页面。
2.在根目录分别建立 控制器文件 index.php 和 模型类文件 smarty.class.php
其中 控制器的功能是:接受用户的输入并调用模型和视图去完成用户的需求。
模型模型表示企业数据和业务规则,这里的功能是接受数据后,读取模板文件,将模板变量替换成用户数据,并生成最终页面文件。
index.php 代码如下:
<?php
//应用根目录
define(\'ROOT_PATH\', realpath(\'./\'));
define(\'DEBUG_MODE\', False);
//模板缓存时间
define(\'CACHE_TIME\', 3600);
if(DEBUG_MODE)
{
$gAppStartTime = microtime(true);
}
//引入模型类
require_once(ROOT_PATH . \'/smarty.class.php\');
//模板目录
$gTpl = new template(\'./template\');
//调用模型类的匹配函数,存入模板变量和值
$gTpl->assign(\'title\', \'Welcome to My World\');
$gTpl->assign(\'content\', \'This is a demo too too\');
$gTpl->display(\'index.php\');
if(DEBUG_MODE)
{
$gAppEndTime = microtime(true);
$runtime = $gAppEndTime - $gAppStartTime;
echo "This Application is running over[".strval($runtime)."s]";
}
?>
3.smarty.class.php代码如下:
<?php
if(!defined(\'ROOT_PATH\')) exit(\'Access Denied\');
class template
{
protected $tpl_dir = \'./tpl\';
//动态页面目录
protected $com_dir = \'./compile\';
//静态页面目录
protected $cache_dir = \'./cache\';
//模板变量和赋值 数组
protected $vars = array();
//类构造函数
function __construct($tpl = \'\', $com = \'\')
{
if($tpl)
{
$this->tpl_dir = $tpl;
}
if($com)
{
$this->com_dir = $com;
}
//判断文件是否具有读取和修改权限
if(!is_writable($this->com_dir) || !is_readable($this->tpl_dir))
{
exit(\'Permission Denied!\');
}
}
function __destruct()
{
}
//将匹配的模板变量名 和 值存入数组
public function assign($varname = \'\', $varvalue = \'\')
{
$this->vars[$varname] = $varvalue;
}
//获取模板源码
protected function fetch_template($tplname = \'\')
{
$tplname = $this->tpl_dir . \'/\' . $tplname;
if(!file_exists($tplname))
{
return \'Template is not Exists!\';
}
if(!is_readable($tplname))
{
return \'File can not read\';
}
$html = file_get_contents($tplname);
return $html;
}
//正则表达式匹配替换模板定义变量
protected function compile_tpl($html = \'\')
{
if(!$html)
{
return;
}
$pattern = \'/(.*?){\$([a-zA-Z_]{1,}\w*)(.*?)}/s\';
return $html = preg_replace($pattern, "\\1<?php echo \$this->vars[\'\\2\']; ?>\\3", $html);
//echo htmlentities($html);
}
//生成结果页面
public function display($tplname)
{
$html = $this->fetch_template($tplname);
$html = $this->compile_tpl($html);
//关闭关闭输出缓冲区
ob_end_flush();
//打开缓冲区
ob_start();
$filename = $this->com_dir . \'/\' . md5($tplname).\'.php\';
//include $filename;
if(!file_exists($filename) || (time() - filectime($filename) > CACHE_TIME) || DEBUG_MODE)
{
file_put_contents($filename, $html);
}
include $filename;
//输出缓冲区内容
$result = ob_get_contents();
//清空缓冲区
ob_end_clean();
$cache_file = $this->cache_dir .\'/\'. md5($tplname) . \'.html\';
if(!file_exists($cache_file) || (time() - filectime($cache_file) > CACHE_TIME) || DEBUG_MODE)
{
file_put_contents($cache_file, $result);
}
include $cache_file;
}
}