zoukankan      html  css  js  c++  java
  • php中mvc框架总结1(7)

    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 首页统一请求代码

    <?php
    //首先载入框架类
    require './framework/Framework.class.php';
    //运行项目
    Framework::run();

     2.3框架类代码

    /**
     * 框架类 初始化基础功能
     */
    class Framework {
        /**
         * 项目框架类的运行入口
         */
        public static function run() {
            self::_initPathConst();//初始化路径常量
            self::_initConfig();//加载配置
            self::_initDispatchParam();//初始化分发参数
            self::_initPlatformPathConst();//初始化平台相关的路径常量
            self::_initAutoload();//注册自动加载方法
            self::_dispatch();//请求分发
        }
    }

    2.3.1初始化路径常量

    /**
         * 初始化路径常量
         */
        private static function _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 static function _initConfig() {
            //载入加载配置文件,并将配置项的值保存与 $config,全局变量中。
            $GLOBALS['config'] = require CFG_PATH . 'application.config.php';
        }

     2.3.3初始化分发参数

    /**
         * 确定p,c,a参数,分发参数,(路由参数)
         */
        private static function _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 static function _initPlatformPathConst() {
            //与当前平台相关的路径常量
            define('CUR_CON_PATH', CON_PATH . $GLOBALS['p'] . '/');//当前平台的控制器目录
            define('CUR_VIE_PATH', VIE_PATH . $GLOBALS['p'] . '/');//当前平台的视图层目录
        }

    2.3.4注册自动加载方法

    private static function _initAutoload() {
            //注册自动加载
            spl_autoload_register(array(__CLASS__, 'selfAutoload'));
        }
    'selfAutoload'方法如下
    public static function 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 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于是将初始化

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

  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/zhenghongxin/p/4339051.html
Copyright © 2011-2022 走看看