文件创建样式:
new_file.php是入口文件,代码为:
<?php//入口文件 被浏览器访问的spl_autoload_register(‘userAutoload‘); ?//注册自动加载函数 ?自动加载函数不用主动调用 在需要的时候自动调用function userAutoload($classname){ ???//定义确定的类和路径 ???$class_list = array( ???‘Controller‘ => FRAMEWORK_PATH.‘Controller.class.php‘, ???‘Factory‘ => FRAMEWORK_PATH.‘Factory.class.php‘, ???‘Model‘ => FRAMEWORK_PATH.‘Model.class.php‘, ???‘MySQLDB‘ => FRAMEWORK_PATH.‘MySQLDB.class.php‘ ???); ???if(isset($class_list[$classname])){ ???????require $class_list[$classname]; ???}elseif(substr($classname, -10) == ‘Controller‘){ ?//判断是否为控制器 ???????require CONREOLLER_PATH.$classname.‘.class.php‘; ???}elseif(substr($classname, -5) == ‘Model‘){ ?//判断是否为模型类 ???????require MODEL_PATH.$classname.‘.class.php‘; ???}}//目录地址常量define(‘ROOT_PATH‘, getcwd().‘/‘); ?//定义根目录define(‘APP_PATH‘,ROOT_PATH.‘application/‘);define(‘FRAMEWORK_PATH‘,ROOT_PATH.‘framework/‘);//控制器类$c = isset($_GET[‘c‘]) ? $_GET[‘c‘] : ‘Match‘;define("PLATFORM", isset($_GET[‘p‘]) ? $_GET[‘p‘] : ‘test‘);//动作$default_action = ‘list‘; ?//默认值$a = isset($_GET[‘a‘]) ? $_GET[‘a‘] : $default_action;//平台相关路径常量define(‘CONREOLLER_PATH‘, APP_PATH.PLATFORM.‘/controller/‘);define(‘MODEL_PATH‘, APP_PATH.PLATFORM.‘/model/‘);define(‘VIEW_PATH‘, APP_PATH.PLATFORM.‘/view/‘);//调用控制器$controller = $c.‘Controller‘;//实例化对象$Match = new $controller();$action = $a.‘Action‘;$Match->$action();
不传参数时p默认为tese c默认为Match a默认为list
实例化一个MatchController对象
MatchController类代码为:
<?phpclass MatchController extends Controller{ ???function listAction(){ ???//查询数据 ???????$match = Factory::getDx(‘MatchModel‘); //用工厂类实例化MatchModel类对象 ???//$match = new MatchModel(); ???$arr = $match->getList(); ???//调用模板 ???require VIEW_PATH.‘match_v.html‘; ???} ???function delAction(){ ???????$id = $_GET[‘id‘]; ???????echo "id是:".$id."<br />"; ???????echo "比赛控制器的删除动作执行了";// ???????header(‘location:new_file.php‘); ???}}
调用MatchController父类Controller(基础控制器)的构造函数:
<?php//基础控制器class Controller{ ???function __construct(){ ???????$this->_initContentType(); ???} ???function _initContentType(){ ???????header(‘Content-Type:text/html; charset=utf-8‘); ???} ???????protected function _jump($url,$info=null,$wait=3){ ???????if($info == ‘‘){ ???????????header(‘location:‘.$url); ???????}else{ ???????????header(‘refresh:‘.$wait.‘;url=‘.$url); ???????????echo $info; ???????} ???????die; ???}}
调用Factory类getDx方法实例化一个MatchModel对象:
<?php//工厂类class Factory{ ???/*判断模型对象有没有 有就直接返回 没有就new一个 ???$model_list = array( ???????‘MatchModel‘ => new MatchModel() ?键是类名,值是这个类的一个对象 ???)*/ ???static function getDx($className){ ?//$className可变类名 ???????static $model_list = array(); ???????if(!isset($model_list[$className])){ ???????????????????????$model_list[$className] = new $className; ???????} ???????return $model_list[$className]; ???}}
工厂类的作用就是单例模型类,传入类名,返回对象
引入MatchModel类,继承Model(基础模型类),当new一个对象的时候,调用Model的构造函数,连接数据库,单例话一个MySQLDB对象存在$this->_dao中,MatchModel对象调用MatchModel方法,执行sql语句,调用视图层显示在页面上:
<!DOCTYPE html><html> ???<head> ???????<meta charset="UTF-8"> ???????<title></title> ???</head> ???<body> ???????<table width="100%" border="1" cellpadding="0" cellspacing="0"> ???????????<tr> ???????????????<th>队伍一</th> ???????????????<th>比分</th> ???????????????<th>队伍二</th> ???????????????<th>时间</th> ???????????</tr> ???????????<!--循环遍历--> ???????????<?php foreach($arr as $v){ ?> ???????????????<tr> ???????????????????<td><a href="new_file.php?c=Team&a=list&id=1&tname=<?php echo $v[‘t1_name‘]; ?>"><?php echo $v[‘t1_name‘]; ?></a></td> ???????????????????<td><?php echo $v[‘t1_score‘].‘:‘.$v[‘t2_score‘]; ?></td> ???????????????????<td><?php echo $v[‘t2_name‘]; ?></td> ???????????????????<td><?php echo date(‘Y-m-d H:i:s‘,$v[‘m_time‘]); ?></td> ???????????????????<td><a href="new_file.php?a=del&id=1">删除</a></td> ???????????????</tr> ???????????<?php } ?> ???????????</table> ???</body></html>
php mvc比赛列表
原文地址:https://www.cnblogs.com/liangdong/p/10409166.html