[PHP] Analysis and design of MVC architecture pattern

Source: Internet
Author: User
Tags php framework

Objective:

This article will show you how to build an MVC mini-framework to understand the architecture and relationships of the MVC (Model-view-control) three layer.

MVC (Model-view-control) Three layer introduction

In order to better distinguish each functional layer development so that the different specialties of the people, so in the efforts of many developers, the framework of MVC as an architectural model more and more, the domestic famous PHP framework has YII2, The frameworks such as Yii and thinkphp, such as the famous PHP framework Laravel, are developed based on the MVC pattern.

Simply put, the view is the presentation layer, such as some HTML format, XML format can serve as a template for the presentation layer, model is a layer of models, such as some specific business logic is done in the model. The controller is the control layer, which chooses which model and which presentation layer, for example, the controller is like a remote control and view is like a television screen, and the model is like a TV show, of course, for example, some inappropriate places

MVC Work Flow

First step Browser---Call the controller and issue instructions to it

Second step controller, select an appropriate model by command

The third step model, according to the controller command to take the corresponding data

Fourth Step controller, select the appropriate view by command

The fifth step view, the data taken by the third step is displayed as the user wants.

Introduction to the mini-MVC framework Build-up

The whole MVC consists of two folders and two files, one class library (LIBS), one library (function), one boot engine (pc.php), and one require file

The concrete implementation of the micro-MVC framework

Function folder

Some functions are stored under this folder, and the size of these functions cannot be directly formed into a class library, but can only be encapsulated into functions and cannot be encapsulated into classes. For example, includes some simple call and instantiation of the controller, model, view method (different from the MySQL class because MySQL corresponds to a series of operations can be encapsulated into class)

Note that when writing these three methods, the Controller method can have two parameters, the model and the view corresponding to only one parameter pit

MySQL a series of operations can be encapsulated into a class, because they are a series of operations corresponding to an object, and the simple call and instantiation of the controller, model, view methods are integrated into a class, their objects can not be unique, so can not be simple call and instantiation of the controller, model, View methods are integrated into a class

Libs folder

The library file for this mini-framework includes

    • Core Library

      A class file that can be called directly and must be used for actual development such as:

      1. Database Class (Note that the database class and database library is different, database library is stored in a specific database of specific operations, and the database class is one of the two parameters is the database type is one of the database library, in short, the database class to operate the specific database)

      2. View class (This class is used to initialize and invoke the view layer of the object, in this framework should be passed to the parameters of this class initialization method is Smaty and Smarty configuration file)

    • Database Library

      Used to store a variety of databases such as MYSQL,MYSQLI,PDO, such as a series of specific operations (refers to additions and deletions) of the code

    • View Gallery

      The Smarty third-party class library is used directly here, and Smarty is a view engine, which is embodied in the display method in Smarty, which can be compiled from the template file (or HTML file) to display it in the view layer.

include.list.php

pc.php

Pc.php is a startup engine program, which initializes the database class, the view class, and initializes the controller and method static variables, instantiates a director and invokes the controller through the two static variables, which is equivalent to the first step in the MVC workflow to call the controller and sends Out instructions

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.