search
Homephp教程php手册php项目中商店mvc框架总结(1),mvc框架
php项目中商店mvc框架总结(1),mvc框架Jun 13, 2016 am 09:12 AM
mvcphpcodeshopSummarizeframeofat presentTable of contentsstructure

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

1.代码结构的划分:

<span>目前的目录结构:
</span>/<span>站点根目录
</span>/application/<span>应用程序目录
    Model</span>/<span>模型目录
    View</span>/<span>视图目录
        Back</span>/<span>后台
        front</span>/<span>
        test</span>/<span>测试平台
    Controller</span>/<span>控制器目录
        Back</span>/<span>后台
        front</span>/<span>前台
        test</span>/<span>测试平台
</span>/framework/<span>框架目录
    MySQLDB.</span><span>class</span><span>.php 数据库操作类DAO
    Model.</span><span>class</span><span>.php 基础模型类
</span>/index.php入口文件

2.请求首页

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

 P=front  <span>//</span><span>后台还是前台 参数有back和front</span>
 C=index   <span>//</span><span>控制器,此处请求首页控制器</span>
 A=shop   <span>//</span><span>动作,此处为首页shop动作</span>

2.2 首页统一请求代码

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

 2.3框架类代码

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

2.3.1初始化路径常量

<span>/*</span><span>*
     * 初始化路径常量
     </span><span>*/</span>
    <span>private</span> <span>static</span><span> function _initPathConst() {
        </span><span>//</span><span>确定项目中使用的路径常量</span>
        define(<span>'</span><span>ROOT_PATH</span><span>'</span>, getCWD() . <span>'</span><span>/</span><span>'</span>);<span>//</span><span>项目的根目录</span>
<span>        
        define(</span><span>'</span><span>APP_PATH</span><span>'</span>, ROOT_PATH . <span>'</span><span>application/</span><span>'</span>);<span>//</span><span>应用程序目录</span>
        define(<span>'</span><span>CON_PATH</span><span>'</span>, APP_PATH . <span>'</span><span>controller/</span><span>'</span>);<span>//</span><span>控制器目录</span>
        define(<span>'</span><span>MOD_PATH</span><span>'</span>, APP_PATH . <span>'</span><span>model/</span><span>'</span>);<span>//</span><span>模型目录</span>
        define(<span>'</span><span>VIE_PATH</span><span>'</span>, APP_PATH . <span>'</span><span>view/</span><span>'</span>);<span>//</span><span>视图层目录</span>
        define(<span>'</span><span>CFG_PATH</span><span>'</span>, APP_PATH . <span>'</span><span>config/</span><span>'</span>);<span>//</span><span>配置文件目录</span>
<span>
        define(</span><span>'</span><span>FRW_PATH</span><span>'</span>, ROOT_PATH . <span>'</span><span>framework/</span><span>'</span>);<span>//</span><span>框架目录</span>
        define(<span>'</span><span>TOL_PATH</span><span>'</span>, FRW_PATH . <span>'</span><span>tool/</span><span>'</span>);<span>//</span><span>工具目录</span>
<span>
        define(</span><span>'</span><span>PUB_PATH</span><span>'</span>, ROOT_PATH . <span>'</span><span>public/</span><span>'</span>);<span>//</span><span>公共资源目录</span>
        define(<span>'</span><span>UPD_PATH</span><span>'</span>, PUB_PATH . <span>'</span><span>upload_image/</span><span>'</span>);<span>//</span><span>上传图片目录</span>
    }

 2.3.2加载配置文件

<span>private</span> <span>static</span><span> function _initConfig() {
        </span><span>//</span><span>载入加载配置文件,并将配置项的值保存与 $config,全局变量中。</span>
        $GLOBALS[<span>'</span><span>config</span><span>'</span>] = require CFG_PATH . <span>'</span><span>application.config.php</span><span>'</span><span>;
    }</span>

 2.3.3初始化分发参数

<span>/*</span><span>*
     * 确定p,c,a参数,分发参数,(路由参数)
     </span><span>*/</span>
    <span>private</span> <span>static</span><span> function _initDispatchParam() {
        </span><span>//</span><span>获得平台参数</span>
        $GLOBALS[<span>'</span><span>p</span><span>'</span>] = $p = isset($_GET[<span>'</span><span>p</span><span>'</span>]) ? $_GET[<span>'</span><span>p</span><span>'</span>] : $GLOBALS[<span>'</span><span>config</span><span>'</span>][<span>'</span><span>app</span><span>'</span>][<span>'</span><span>default_platform</span><span>'</span>];<span>//</span><span>p,platform
        </span><span>//</span><span>获得控制器类参数</span>
        $GLOBALS[<span>'</span><span>c</span><span>'</span>] = isset($_GET[<span>'</span><span>c</span><span>'</span>]) ? $_GET[<span>'</span><span>c</span><span>'</span>] : $GLOBALS[<span>'</span><span>config</span><span>'</span>][$p][<span>'</span><span>default_controller</span><span>'</span>];<span>//</span><span>c,controller
        </span><span>//</span><span>获得动作参数</span>
        $GLOBALS[<span>'</span><span>a</span><span>'</span>] = isset($_GET[<span>'</span><span>a</span><span>'</span>]) ? $_GET[<span>'</span><span>a</span><span>'</span>] : $GLOBALS[<span>'</span><span>config</span><span>'</span>][$p][<span>'</span><span>default_action</span><span>'</span>];<span>//</span><span>a,action</span>
    }

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

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

<span>/*</span><span>*
     * 初始化当前平台相关的路径常量
     * 这个是用来判断P的,找到究竟是哪个控制下
     </span><span>*/</span>
    <span>private</span> <span>static</span><span> function _initPlatformPathConst() {
        </span><span>//</span><span>与当前平台相关的路径常量</span>
        define(<span>'</span><span>CUR_CON_PATH</span><span>'</span>, CON_PATH . $GLOBALS[<span>'</span><span>p</span><span>'</span>] . <span>'</span><span>/</span><span>'</span>);<span>//</span><span>当前平台的控制器目录</span>
        define(<span>'</span><span>CUR_VIE_PATH</span><span>'</span>, VIE_PATH . $GLOBALS[<span>'</span><span>p</span><span>'</span>] . <span>'</span><span>/</span><span>'</span>);<span>//</span><span>当前平台的视图层目录</span>
    }

2.3.4注册自动加载方法

<span>private</span> <span>static</span><span> function _initAutoload() {
        </span><span>//</span><span>注册自动加载</span>
        spl_autoload_register(array(__CLASS__, <span>'</span><span>selfAutoload</span><span>'</span><span>));
    }
</span><span>'</span><span>selfAutoload</span><span>'</span><span>方法如下
</span><span>public</span> <span>static</span><span> function selfAutoload($class_name) {
        </span><span>//</span><span>先判断是否为框架核心类,框架中可以被确定的类</span>
        $class_file =<span> array(
            </span><span>'</span><span>Model</span><span>'</span> => FRW_PATH . <span>'</span><span>Model.class.php</span><span>'</span><span>,
            </span><span>'</span><span>MySQLDB</span><span>'</span> => FRW_PATH . <span>'</span><span>MySQLDB.class.php</span><span>'</span><span>,
            </span><span>'</span><span>Controller</span><span>'</span> => FRW_PATH . <span>'</span><span>Controller.class.php</span><span>'</span><span>,
            </span><span>'</span><span>SessionDB</span><span>'</span> => TOL_PATH . <span>'</span><span>SessionDB.class.php</span><span>'</span><span>,
            </span><span>'</span><span>Captcha</span><span>'</span> => TOL_PATH . <span>'</span><span>Captcha.class.php</span><span>'</span><span>,
            </span><span>'</span><span>Upload</span><span>'</span> => TOL_PATH . <span>'</span><span>Upload.class.php</span><span>'</span><span>,
            </span><span>'</span><span>Image</span><span>'</span> => TOL_PATH . <span>'</span><span>Image.class.php</span><span>'</span><span>,
            </span><span>'</span><span>Page</span><span>'</span> => TOL_PATH . <span>'</span><span>Page.class.php</span><span>'</span><span>,
        );
        </span><span>if</span><span> (isset($class_file[$class_name])) {
            </span><span>//</span><span>是核心类</span>
<span>            require $class_file[$class_name];
        }    
        </span><span>//</span><span>是否为模型类</span>
        elseif (substr($class_name, -<span>5</span>) == <span>'</span><span>Model</span><span>'</span><span>) {
            </span><span>//</span><span>模型类</span>
            require MOD_PATH . $class_name . <span>'</span><span>.class.php</span><span>'</span><span>;
        }
        </span><span>//</span><span>是否为控制器类</span>
        elseif (substr($class_name, -<span>10</span>) == <span>'</span><span>Controller</span><span>'</span><span>) {
            </span><span>//</span><span>控制器类</span>
            require CUR_CON_PATH . $class_name . <span>'</span><span>.class.php</span><span>'</span><span>;
        }
    }</span>

2.3.4 请求分发

<span>/*</span><span>*
     * 请求分发
     * 将请求交由 某个控制器的某个动作完成
     </span><span>*/</span>
    <span>private</span> <span>static</span><span> function _dispatch() {
        </span><span>//</span><span>实例化控制器类,与 调用相应的动作方法
        </span><span>//</span><span>ucfirst() 函数把字符串中的首字符转换为大写。</span>
        $controller_name = ucfirst($GLOBALS[<span>'</span><span>c</span><span>'</span>]) . <span>'</span><span>Controller</span><span>'</span>;<span>//</span><span>match Match . Controller
        </span><span>//</span><span>载入控制器类</span>
        $controller = <span>new</span> $controller_name;<span>//</span><span>可变类名

        </span><span>//</span><span>调用动作方法</span>
        $action_name = $GLOBALS[<span>'</span><span>a</span><span>'</span>] . <span>'</span><span>Action</span><span>'</span><span>;
        $controller</span>->$action_name();<span>//</span><span>可变方法</span>
    }

2.3.5当我们请求localhost/index.php的时候,相当于请求localhost/index.php?p=front&c=shop&a=index于是将初始化

     application\controller\front下的ShopController控制器,请求动作为indexAction

     indexAction代码如下:

<span>public</span><span> function indexAction() {
        </span><span>//</span><span>得到分类数据</span>
        $model_cat = <span>new</span><span> CatModel;
        $cat_list </span>= $model_cat-><span>getNestedList();
        </span><span>//</span><span>载入前台首页模板</span>
        require CUR_VIE_PATH . <span>'</span><span>index.html</span><span>'</span><span>;
    }</span>

需要说明的是:

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

关系如下:

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

   $model_cat = <span>new</span><span> CatModel;   &mdash;&mdash;》 便是实例化catModel类
    $cat_list </span>= $model_cat->getNestedList();  &mdash;&mdash;》取得所有前台分类

3、在基础模型中,封装好所有基础操作数据库方法,其中getNestedLIst方法如下

<span>/*</span><span>*
     * 得到嵌套的分类列表数据
     </span><span>*/</span>
    <span>public</span> function getNestedList($p_id=<span>0</span><span>) {
        </span><span>//</span><span>获得所有分类</span>
        $list = $<span>this</span>-><span>getList();
        </span><span>//</span><span>制作嵌套的数据,递归查找</span>
        <span>return</span> $<span>this</span>-><span>getNested($list, $p_id);
    }</span>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft