仁者爱人

今日努力学习,是为了明日衣食无忧.

导航

phpcms v9开源开发框架基础mvc解读

  • 根据对mvc的了解
  • 简要写个mvc框架 也谈不上框架 希望对新手有帮助
  • 简单的解析下mvc  你可以这样了解 m模型也就是数据库操作 v视图  c控制器 通过url来判断调用m和v来完成请求,本身没数据库操作。
  • 根目录(现在利用v9文件格式)
  • index.php 入口文件
  • |  –  api  接口文件目录
  • |  –  caches 缓存文件目录
  •        |  – configs 系统配置文件目录
  •        |  – caches_* 系统缓存目录
  • |  –  phpcms  phpcms框架主目录
  •        |  – languages 框架语言包目录
  •        |  – libs 框架主类库、主函数库目录
  •        |  – model 框架数据库模型目录
  •        |  – modules 框架模块目录
  •        |  – templates 框架系统模板目录
  •        |  – base.php 框架主程序
  • 首先根目录新建一个index.php 编码随意 我这里用utf-8
  • 内容如下
  • define('root',dirname(__file__).directory.seprartor);//根目录
  • include root.'phpcms/base.php'; //包含根目录phpcms文件夹下面的base.php
  • base:run(); //默认加载base类下面的静态run方法。
  • 下面是base.php //位于phpcms下面。
  • 内容如下
  • define('pc_path',dirname(__file__).directory.seprartor);//框架主目录
  • //其他定义省略
  • class base
  • {
  • //定义初始化方法
  • public static function run()
  • {
  •   echo '默认加载我';
  •   自动载入app类
  •   base::load_sys_class('app'); app文件内容请看下面
  •   
  • }
  • //定义加载系统类的方法$classname类名称,$path地址,$new是否初始化
  • public static load_sys_class($classname,$path='',$new=1)
  • {
  •   if($path=='') $path = 'libs/classes/';   //如果地址不存在 自动选择到系统类文件夹下
  •   if(file_exists(pc_path.$path.$classname.'class.php')) //存在该文件
  •   {
  •    include  pc_path.$path.$classname.'class.php'; //包含他
  •    if($new)
  •    {
  •     return new $classname;   //初始化
  •    }
  •    else
  •    {
  •     return true;
  •    }
  •   }
  •   else
  •   {
  •    文件不存在,请建立;或者直接 return false;
  •   }
  • }
  • }
  • app.class.php  位于phpcms/lib/classes 系统类
  • 内容如下
  • class app
  • {
  • 首先定义一个构造函数 让程序默认加载
  • public function __construct()
  • {
  •   echo '默认加载到这里咯';//打开index。php就会看到这段话。
  •   //初始化一些值
  •   define('route_m',$_request['m'] ? $_request['m'] : 'content'); //如果有传递m就赋值 否则默认给个值
  •   define('route_c',$_request['c'] ? $_request['c'] : 'index'); //如果有传递c就赋值 否则默认给个值
  •   define('route_a',$_request['a'] ? $_request['a'] : 'init'); //如果有传递a就赋值 否则默认给个值
  •   $this->init();//默认执行
  • }
  • //以下两个方法引用官方 自己写也就和那个意思一样 偷懒
  •   /**
  •   * 调用件事
  •   */
  • private function init() {
  •   $controller = $this->load_controller();//这里已经是对象了
  •   if (method_exists($controller, route_a)) {
  •    if (preg_match('/^[_]/i', route_a)) {
  •     exit('you are visiting the action is to protect the private action');
  •    } else {
  •     call_user_func(array($controller, route_a)); //?index.php?m=content&c=index&a=init 默认url   主要判断init是否存在index类里面的一个方法 是的话就调用 这个就是c做的事情根据url来操作对应的m和a
  •    }
  •   } else {
  •    exit('action does not exist.');
  •   }
  • }
  • /**
  •   * 加载控制器
  •   * @param string $filename
  •   * @param string $m
  •   * @return obj
  •   */
  • private function load_controller($filename = '', $m = '') {
  •   if (empty($filename)) $filename = route_c;
  •   if (empty($m)) $m = route_m;
  •   $filepath = pc_path.'modules'.directory_separator.$m.directory_separator.$filename.'.php'; //地址就是phpcms/modules/content/index.php  为什么是这个地址 因为是模块开发
  •   if (file_exists($filepath)) {
  •    $classname = $filename;
  •    include $filepath;
  •    
  •    return new $classname; //返回new过的对象
  •   } else {
  •    exit('controller does not exist.');
  •   }
  • }
  • }
  • 这样打开index。php默认就运行的 phpcms/modules/content/index.php  index类下面的init方面
  • 下面看看他的内容怎么写
  • class index
  • {
  • 设置一个连接数据库属性
  • private $db  = null;
  • public function __construct()
  • {
  •   初始化一些值
  •   写一个连接数据库的类mysql
  •   include 那个数据库类;
  •   $this->db= new mysql();
  •   $this->db->connect('');连接数据库 具体自己写写
  • }
  • //默认加载
  • public function init()
  • {
  •   echo '默认就加载到我这里了';
  •   $result = $this->db->query('select * from v9_admin');
  •   while($res=$this->db->fetch($result))
  •   {
  •    print_r($res);
  •   }
  •   然后
  •   include 模板文件 //
  •   
  •   以上就是简单的实现一些mvc。
  • }

posted on 2013-10-09 11:25  semcoding  阅读(1768)  评论(0编辑  收藏  举报