php项目中商店mvc框架总结(1),mvc框架1.代码结构的划分:
目前的目录结构:/站点根目录/application/应用程序目录 model/模型目录 view/视图目录 back/后台 front/ test/测试平台 controller/控制器目录 back/后台 front/前台 test/测试平台/framework/框架目录 mysqldb.class.php 数据库操作类dao model.class.php 基础模型类/index.php入口文件
2.请求首页
2.1请求首页参数实例(请求localhost/index.php?p=front&c=shop&a=index)
p=front //后台还是前台 参数有back和front c=index //控制器,此处请求首页控制器 a=shop //动作,此处为首页shop动作
2.2 首页统一请求代码
frw_path . 'model.class.php', 'mysqldb' => frw_path . 'mysqldb.class.php', 'controller' => frw_path . 'controller.class.php', 'sessiondb' => tol_path . 'sessiondb.class.php', 'captcha' => tol_path . 'captcha.class.php', 'upload' => tol_path . 'upload.class.php', 'image' => tol_path . 'image.class.php', 'page' => tol_path . 'page.class.php', ); if (isset($class_file[$class_name])) { //是核心类 require $class_file[$class_name]; } //是否为模型类 elseif (substr($class_name, -5) == 'model') { //模型类 require mod_path . $class_name . '.class.php'; } //是否为控制器类 elseif (substr($class_name, -10) == 'controller') { //控制器类 require cur_con_path . $class_name . '.class.php'; } }
2.3.4 请求分发
/** * 请求分发 * 将请求交由 某个控制器的某个动作完成 */ private static function _dispatch() { //实例化控制器类,与 调用相应的动作方法 //ucfirst() 函数把字符串中的首字符转换为大写。 $controller_name = ucfirst($globals['c']) . 'controller';//match match . controller //载入控制器类 $controller = new $controller_name;//可变类名 //调用动作方法 $action_name = $globals['a'] . 'action'; $controller->$action_name();//可变方法 }
2.3.5当我们请求localhost/index.php的时候,相当于请求localhost/index.php?p=front&c=shop&a=index于是将初始化
application\controller\front下的shopcontroller控制器,请求动作为indexaction
indexaction代码如下:
public function indexaction() { //得到分类数据 $model_cat = new catmodel; $cat_list = $model_cat->getnestedlist(); //载入前台首页模板 require cur_vie_path . 'index.html'; }
需要说明的是:
1、shopcontroller继承与平台控制器platformcontroller,平台控制器继承于基础控制器类:controller
关系如下:
2、在确定好mvc中的,control动作后,接下来就是实现model
$model_cat = new catmodel; ——》 便是实例化catmodel类 $cat_list = $model_cat->getnestedlist(); ——》取得所有前台分类
3、在基础模型中,封装好所有基础操作数据库方法,其中getnestedlist方法如下
/** * 得到嵌套的分类列表数据 */ public function getnestedlist($p_id=0) { //获得所有分类 $list = $this->getlist(); //制作嵌套的数据,递归查找 return $this->getnested($list, $p_id); }
4、getlist方法如下
/** * 获得列表数据 */ public function getlist() { $sql = select * from `php_category`; return $this->_db->fetchall($sql); }
5、model实现好之后,就是载入view
//载入前台首页模板 require cur_vie_path . 'index.html';
2.3.6 总结:实现一个功能,首先确定control,然后实现model,最后载入view
2.3.7效果图 前台页面不加以阐述
http://www.bkjia.com/phpjc/967693.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/967693.htmltecharticlephp项目中商店mvc框架总结(1),mvc框架 1 .代码结构的划分: 目前的目录结构: / 站点根目录 /application/ 应用程序目录 model / 模型目录...
