上节介绍了框架核心的核心启动类
本次了解Controller(控制器基类)、Model(模型基类)
Model.class.php
模型基类
被所有模型类继承,这里封装了Model类共用的方法,例如:数据库初始化连接、数据库操作的基本方法
<?php ???class Model{ ???????protected $db; //数据库连接对象 ???????protected $table; //表名 ???????protected $fields = array(); ?//字段列表 ???????//实例化时要求传入表名 ???????public function __construct($table){ ???????????//读取配置文件中数据库配置项,创建数据库连接对象 ???????????$dbconfig[‘host‘] = $GLOBALS[‘config‘][‘host‘]; ???????????$dbconfig[‘user‘] = $GLOBALS[‘config‘][‘user‘]; ???????????$dbconfig[‘password‘] = $GLOBALS[‘config‘][‘password‘]; ???????????$dbconfig[‘dbname‘] = $GLOBALS[‘config‘][‘dbname‘]; ???????????$dbconfig[‘port‘] = $GLOBALS[‘config‘][‘port‘]; ???????????$dbconfig[‘charset‘] = $GLOBALS[‘config‘][‘charset‘]; ???????????????????????$this->db = new Mysql($dbconfig); ???????????$this->table = $GLOBALS[‘config‘][‘prefix‘] . $table; ???????} ???????/** ????????* 插入方法 ????????* @param string $tbName 操作的数据表名 ????????* @param array $data 字段-值的一维数组 ????????* @return int 受影响的行数 ????????*/ ???????public function insert(array $data){ ???????????return $this->db->insert($this->table,$data); ???????} ???????????/** ????????* 删除方法 ????????* @param string $tbName 操作的数据表名 ????????* @return int 受影响的行数 ????????*/ ???????public function delete() { ???????????return $this->db->delete($this->table); ???????} ???????????/** ????????* 更新函数 ????????* @param string $tbName 操作的数据表名 ????????* @param array $data 参数数组 ????????* @return int 受影响的行数 ????????*/ ???????public function update(array $data) { ???????????return $this->db->update($this->table,$data); ???????} ???????????/** ????????* 查询函数 ????????* @param string $tbName 操作的数据表名 ????????* @return array 结果集 ????????*/ ???????public function select() { ???????????return $this->db->select($this->table); ???????} ???????????/** ????????* @param mixed $option 组合条件的二维数组,例:$option[‘field1‘] = array(1,‘=>‘,‘or‘) ????????* @return $this ????????*/ ???????public function where($option) { ???????????$this->db->where($option); ???????????return $this; ???????} ???????????/** ????????* 设置排序 ????????* @param mixed $option 排序条件数组 例:array(‘sort‘=>‘desc‘) ????????* @return $this ????????*/ ???????public function order($option) { ???????????$this->db->order($option); ???????????return $this; ???????} ???????????????/** ????????* 设置查询行数及页数 ????????* @param int $page pageSize不为空时为页数,否则为行数 ????????* @param int $pageSize 为空则函数设定取出行数,不为空则设定取出行数及页数 ????????* @return $this ????????*/ ???????public function limit($page,$pageSize=null) { ???????????$this->db->limit($page,$pageSize); ???????????return $this; ???????} ???????????????/** ????????* 设置查询字段 ????????* @param mixed $field 字段数组 ????????* @return $this ????????*/ ???????public function field($field){ ???????????$this->db->field($field); ???????????return $this; ???????} ???}
Controller.class.php
控制器基类
所用控制器的基类,封装控制器共用的方法,例如:跳转、展示模板、给模板赋值、ajax返回、重定向等
<?php ???class Controller { ???????public function jump($url,$message,$wait){ ???????????if($wait == 0){ ???????????????header("Location:$url"); ???????????} else { ???????????????include CUR_VIEW_PATH . "message.html"; ???????????} ???????????//要强制退出 ???????????exit(); ???????} ???}
应用中,让BaseController继承Controller类,在BaseController中显示访问权限控制,后台所有控制器继承BaseController
所有Model类继承Model,在model层中实现对数据库的访问和数据处理。在控制器中实例化model模型,调用Model层方法访问数据库,并对返回数据进行处理(展示页面,或者返回数据)
PHP深入解读MVC框架(二)
原文地址:http://www.cnblogs.com/xiaoliwang/p/7963310.html