现在web应用普遍使用的是MVC设计模式,即模型(Model)、视图(View)和控制器(Controller)
搭建框架首先要明白三者的联系,我们可以设计一个单入口文件模式,具体流程是
index.php->获取参数得到Controller控制器名,method方法名,param参数->构造控制器->构造相应的视图、模型->调用相应的方法
具体步骤
1、首先建立一个项目YHPHP,在项目下建立如下目录app、config、library、tmp
app下建立如下目录Controllers,Models,Templates,其中分别放入的是控制器,模型,视图
在Controllers目录下建立Controller.class.php、IndexController.class.php ,控制器文件格式名为XXXXController.class.php,Controller是所有控制类的基类
在Templates目录下创建Template.class.php文件,具体作用稍后再说
在Models目录下创建一个处理数据库的类Database,这个类添加对数据库的链接访问,已经常用的增删改查,然后还在该目录下创建Model.class.php类 来继承Database
在libray目录下创建shared.php文件,common.php文件,common主要放置自定义的全局函数
config下建立config.php文件,其中主要是数据库和项目的配置
2、在YHPHP根目录下创建index.php入口文件,具体内容如下
define('DS',DIRECTORY_SEPARATOR);
define('ROOT',dirname(__FILE__));
require_once(ROOT.DS.'config'.DS.'config.php');
require_once(ROOT.DS.'library'.DS.'shared.php');
?>
3、打开第一步创建的shared.php文件,录入以下代码
require_once(ROOT.DS."library".DS."common.php");
//类魔术函数
function __autoload($classname){
$classpath = ROOT.DS."app".DS."Controllers".DS.$classname.".class.php";
$classpath_Temp = ROOT.DS."app".DS."Templates".DS.$classname.".class.php";
$classpath_Model = ROOT.DS."app".DS."Models".DS.$classname.".class.php";
if(file_exists($classpath)){
require_once($classpath);
}else if(file_exists($classpath_Temp)){
require_once($classpath_Temp);
}else if(file_exists($classpath_Model)){
require_once($classpath_Model);
}else{
echo 'class file'.$classpath.'not found!';
die;
}
}
function main() {
$Control = isset($_GET["c"])?$_GET["c"]:"Index";
$Method = isset($_GET["m"])?$_GET["m"]:"index";
$Cname = $Control;
$Control = $Control.'Controller';
$dispatch = new $Control($Cname,$Method);
if(method_exists($Control,$Method)){
call_user_func(array($dispatch,$Method));
}else{
echo "没找到".$Method."方法";
}
}
main();main函数作用是得到url中的控制器名、方法名、已经参数,url形如localhost/index.php?c=Index&m=hello&name=wei
然后使用$dispatch = new $Control($Cname,$Method); 出一个对象实例,这一步看起来非常奇怪,我们没有include类的路径,怎么就可以创建对象呢?
大家仔细看前面还有个__autoload方法,这个方法会在你实例化对象之前自动加载指定的文件,也就是说在实力对象前,会调用__autoload方法,参数就是类的名称,在__autoload中把类包含进来就可以了
然后使用method_exists函数查看类中是否存在该方法,如果存在调用这个方法。
4、然后打开控制器的基类Controller.class.php,录入以下代码
class Controller{
protected $cname;
protected $method;
protected $temp;
public function __construct($cname,$method){
$this->cname = $cname;
$this->method = $method;
$this->temp = new Template($cname,$method);
}
//设置view变量
function setVar($name,$value){
$this->temp->setVar($name,$value);
}
function __destruct() {
$this->temp->render();
}
}
该类在构造的时候有创建了一个Template、Model的实例,然后在析构中调用Template类的render方法,加载对应的模板
5、打开Template.class.php文件录入以下代码
class Template {
//变量
protected $variables = array();
protected $cname;
protected $method;
function __construct($cname,$method) {
$this->cname = $cname;
$this->method =$method;
}
function setVar($name,$value){
$this->variables[$name] = $value;
}
function render(){
//数组导入变量
extract($this->variables);
$classpath_Temp = ROOT.DS."app".DS."Templates".DS.$this->cname.DS.$this->method.".php";
if(file_exists($classpath_Temp)){
require_once($classpath_Temp);
}else{
echo "未找到".$classpath_Temp;
}
}
}
这个类中render方法根据控制层传来的cname加载相应的view视图,仔细看$classpath_Temp = ROOT.DS."app".DS."Templates".DS.$this->cname.DS.$this->method.".php";这段代码,意思是选择
Templates目录cname目录下的method.php文件,也就是说在Templates目录下创建控制器名为子目录,方法名为文件的view视图
这样视图和控制层我们已经完成,涉及到对数据库的操作,我们可以像TP那样定义一个全局函数M来访问数据库,在commone.php中添加一个M函数
function M($name){
return new Model($name);
}
打开
Database类,添加对数据库的常用操作
class Database{
protected $_dbHandle;
protected $_result;
protected $_table;
/** 连接数据库 **/
function connect($address, $account, $pwd, $name) {
$this->_dbHandle = @mysql_connect($address, $account, $pwd);
if ($this->_dbHandle != 0) {
if (mysql_select_db($name, $this->_dbHandle)) {
return 1;
}else {
return 0;
}
}else {
return 0;
}
}
/** 中断数据库连接 **/
function disconnect() {
if (@mysql_close($this->_dbHandle) != 0) {
return 1;
} else {
return 0;
}
}
/** 查询所有数据表内容 **/
function selectAll() {
$query = 'select * from `'.$this->_table.'`';
return $this->query($query);
}
/** 查询数据表指定列内容 **/
function select($id) {
$query = 'select * from `'.$this->_table.'` where `id` = \''.mysql_real_escape_string($id).'\'';
return $this->query($query, 1);
}
/** 自定义SQL查询语句 **/
function query($query, $singleResult = 0) {
$this->_result = mysql_query($query, $this->_dbHandle);
if (preg_match("/select/i",$query)) {
$result = array();
$table = array();
$field = array();
$tempResults = array();
$numOfFields = mysql_num_fields($this->_result);
for ($i = 0; $i < $numOfFields; ++$i) {
array_push($table,mysql_field_table($this->_result, $i));
array_push($field,mysql_field_name($this->_result, $i));
}
while ($row = mysql_fetch_row($this->_result)) {
for ($i = 0;$i < $numOfFields; ++$i) {
$table[$i] = trim(ucfirst($table[$i]),"s");
$tempResults[$table[$i]][$field[$i]] = $row[$i];
}
if ($singleResult == 1) {
mysql_free_result($this->_result);
return $tempResults;
}
array_push($result,$tempResults);
}
mysql_free_result($this->_result);
return($result);
}
}
/** 返回结果集行数 **/
function getNumRows() {
return mysql_num_rows($this->_result);
}
/** 释放结果集内存 **/
function freeResult() {
mysql_free_result($this->_result);
}
/** 返回MySQL操作错误信息 **/
function getError() {
return mysql_error($this->_dbHandle);
}
}
添加Model类继承Database类
class Model extends Database{
function __construct($tableName) {
$this->connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$this->_table = $tableName;
}
function __destruct() {
}
}
这样MVC的雏形基本完成了,接下来我们来测试一下,我们可以创建一个IndexController控制器类,然后在Templates下 创建一个Index目录和index.php文件
创建一个数据库,创建一个user表,我们用IndexController来查询表
class IndexController extends Controller{
function index(){
$user = M("user");
$arr = $user->selectAll();
print_r($arr);
}
}想要PHP和html分离,我们可以使用smarty框架,可以参考
http://www.cnblogs.com/languoliang/articles/2850276.html这篇文章整合