文件创建样式:
new_file.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类代码为:
class 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."
";
echo "比赛控制器的删除动作执行了";
// header('location:new_file.php');
}
}
调用MatchController父类Controller(基础控制器)的构造函数:
//基础控制器
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对象:
//工厂类
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语句,调用视图层显示在页面上:
队伍一 | 比分 | 队伍二 | 时间 | |
---|---|---|---|---|
<?php echo $v['t1_name']; ?> | <?php echo $v['t1_score'].':'.$v['t2_score']; ?> | <?php echo $v['t2_name']; ?> | <?php echo date('Y-m-d H:i:s',$v['m_time']); ?> | 删除 |
标签:GET,list,列表,mvc,PATH,php,class,define
来源: https://www.cnblogs.com/liangdong/p/10409166.html