php商店框架,php项目中商店mvc框架总结(1)

1.代码结构的划分:

目前的目录结构:/站点根目录/application/应用程序目录

Model/模型目录

View/视图目录

Back/后台

front/test/测试平台

Controller/控制器目录

Back/后台

front/前台

test/测试平台/framework/框架目录

MySQLDB.class.php 数据库操作类DAO

Model.class.php 基础模型类/index.php入口文件

907586d51ff47613eda67f4e0baec495.png

2.请求首页

2.1请求首页参数实例(请求localhost/index.php?p=front&c=shop&a=index)

P=front //后台还是前台 参数有back和front

C=index //控制器,此处请求首页控制器

A=shop //动作,此处为首页shop动作

2.2 首页统一请求代码

require ‘./framework/Framework.class.php‘;//运行项目

Framework::run();

2.3框架类代码

/**

* 框架类 初始化基础功能*/

classFramework {/**

* 项目框架类的运行入口*/

public staticfunction run() {

self::_initPathConst();//初始化路径常量

self::_initConfig();//加载配置

self::_initDispatchParam();//初始化分发参数

self::_initPlatformPathConst();//初始化平台相关的路径常量

self::_initAutoload();//注册自动加载方法

self::_dispatch();//请求分发

}

}

2.3.1初始化路径常量

/**

* 初始化路径常量*/

private staticfunction _initPathConst() {//确定项目中使用的路径常量

define(‘ROOT_PATH‘, getCWD() . ‘/‘);//项目的根目录

define(‘APP_PATH‘, ROOT_PATH . ‘application/‘);//应用程序目录

define(‘CON_PATH‘, APP_PATH . ‘controller/‘);//控制器目录

define(‘MOD_PATH‘, APP_PATH . ‘model/‘);//模型目录

define(‘VIE_PATH‘, APP_PATH . ‘view/‘);//视图层目录

define(‘CFG_PATH‘, APP_PATH . ‘config/‘);//配置文件目录

define(‘FRW_PATH‘, ROOT_PATH . ‘framework/‘);//框架目录

define(‘TOL_PATH‘, FRW_PATH . ‘tool/‘);//工具目录

define(‘PUB_PATH‘, ROOT_PATH . ‘public/‘);//公共资源目录

define(‘UPD_PATH‘, PUB_PATH . ‘upload_image/‘);//上传图片目录

}

2.3.2加载配置文件

private staticfunction _initConfig() {//载入加载配置文件,并将配置项的值保存与 $config,全局变量中。

$GLOBALS[‘config‘] = require CFG_PATH . ‘application.config.php‘;

}

2.3.3初始化分发参数

/**

* 确定p,c,a参数,分发参数,(路由参数)*/

private staticfunction _initDispatchParam() {//获得平台参数

$GLOBALS[‘p‘] = $p = isset($_GET[‘p‘]) ? $_GET[‘p‘] : $GLOBALS[‘config‘][‘app‘][‘default_platform‘];//p,platform//获得控制器类参数

$GLOBALS[‘c‘] = isset($_GET[‘c‘]) ? $_GET[‘c‘] : $GLOBALS[‘config‘][$p][‘default_controller‘];//c,controller//获得动作参数

$GLOBALS[‘a‘] = isset($_GET[‘a‘]) ? $_GET[‘a‘] : $GLOBALS[‘config‘][$p][‘default_action‘];//a,action

}

以上代码中用到了初始加载配置文件,初始化默认请求,当你直接请求:localhost/index.php,没有参数的时候,加载系统默认参数

2.3.4初始化平台相关的路径常量

/**

* 初始化当前平台相关的路径常量

* 这个是用来判断P的,找到究竟是哪个控制下*/

private staticfunction _initPlatformPathConst() {//与当前平台相关的路径常量

define(‘CUR_CON_PATH‘, CON_PATH . $GLOBALS[‘p‘] . ‘/‘);//当前平台的控制器目录

define(‘CUR_VIE_PATH‘, VIE_PATH . $GLOBALS[‘p‘] . ‘/‘);//当前平台的视图层目录

}

2.3.4注册自动加载方法

private staticfunction _initAutoload() {//注册自动加载

spl_autoload_register(array(__CLASS__, ‘selfAutoload‘));

}‘selfAutoload‘方法如下public staticfunction selfAutoload($class_name) {//先判断是否为框架核心类,框架中可以被确定的类

$class_file =array(‘Model‘ => 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 staticfunction _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代码如下:

publicfunction indexAction() {//得到分类数据

$model_cat = newCatModel;

$cat_list= $model_cat->getNestedList();//载入前台首页模板

require CUR_VIE_PATH . ‘index.html‘;

}

需要说明的是:

1、ShopController继承与平台控制器PlatformController,平台控制器继承于基础控制器类:controller

关系如下:

806355b334566b1ceb136e6a4ddb1745.png

2、在确定好MVC中的,Control动作后,接下来就是实现Model

$model_cat = newCatModel; ——》 便是实例化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方法如下

/**

* 获得列表数据*/

publicfunction 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效果图  前台页面不加以阐述

a1c8c4adec336c4834f263720a162be5.png

原文:http://www.cnblogs.com/zhenghongxin/p/4339051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值