search
HomeBackend DevelopmentPHP TutorialSummary of mvc framework for stores in php projects (1), mvc framework_PHP tutorial

Summary of mvc framework for stores in php projects (1), mvc framework_PHP tutorial

Jul 13, 2016 am 10:03 AM
mvcphpcodeshopSummarizeframeofat presentTable of contentsstructure

Summary of store mvc framework in PHP project (1), mvc framework

1. Division of code structure:

<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. Request Home Page

2.1 Request homepage parameter example (request 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 Unified request code for homepage

<?<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 Framework code

<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 Initialization path constants

<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 Load configuration file

<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 Initialization distribution parameters

<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>
    }

The above code uses the initial loading configuration file to initialize the default request. When you directly request: localhost/index.php without parameters, the system default parameters are loaded

2.3.4 Initializing platform-related path constants

<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 Registration automatic loading method

<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 Request Distribution

<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 When we request localhost/index.php, it is equivalent to requesting localhost/index.php?p=front&c=shop&a=index and then initialize

The ShopController controller under applicationcontrollerfront, the request action is indexAction

The indexAction code is as follows:

<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>

What needs to be explained is:

1. ShopController inherits from PlatformController, and the platform controller inherits from the basic controller class: controller

The relationship is as follows:

2. After determining the Control action in MVC, the next step is to implement the Model

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

3. In the basic model, all basic operation database methods are encapsulated, among which the getNestedLIst method is as follows

<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>

4. The getList method is as follows

 <span>/*</span><span>*
     * 获得列表数据
     </span><span>*/</span>
    <span>public</span><span> function getList() {
        $sql </span>= <span>"</span><span>select * from `php_category`</span><span>"</span><span>;
        </span><span>return</span> $<span>this</span>->_db-><span>fetchAll($sql);
    }</span>

5. After the Model is implemented, load the View

    <span>//</span><span>载入前台首页模板</span>
    require CUR_VIE_PATH . <span>'</span><span>index.html</span><span>'</span>;

2.3.6 Summary: To implement a function, first determine the Control, then implement the Model, and finally load the View

2.3.7 renderings will not be explained on the front page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/967693.htmlTechArticleSummary of store mvc framework in php project (1), mvc framework 1. Division of code structure: Current directory structure: / Site root directory / application / Application directory Model / Model directory...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software