[PHP] MVC architecture pattern analysis and design-php Tutorial

Source: Internet
Author: User
[PHP] MVC architecture pattern analysis and design:

This article will introduce how to build a micro-framework of MVC to understand the three-layer architecture and relationship of MVC (model-view-control ).

Introduction to Model-View-Control (MVC)

In order to better differentiate the development of different functional layers so that people with different expertise can perform their respective duties, with the efforts of many developers, there are more and more frameworks adopting the MVC architecture model, the well-known PHP frameworks in China include Yii2, Yii, ThinkPHP, and other well-known PHP frameworks such as Laravel, which are developed based on the MVC model.

Simply put, a View is a presentation layer. for example, some html and xml formats can all act as templates for this presentation layer. a Model is a Model layer, for example, some specific business logic is completed in the model. The Controller is the Controller layer. it selects the Model and performance layer. for example, the Controller is like a remote control and the View is like a TV screen, while the Model is like a TV program, of course, for example, some are not suitable.

MVC workflow

Step 1 viewer-> call the controller and issue a command to it

Step 2 controller-> select an appropriate model by command

Step 3 model-> get the corresponding data according to the controller command

Step 4 controller-> select the view according to the command

Step 5 View-> display the data obtained in step 3 as the user wants

Introduction to the creation of the micro MVC framework

The entire MVC consists of two folders and two files: a class library (libs), a function library (function), a startup engine (pc. php), and a require file.

Function folder of the micro MVC framework

Some functions are stored in this folder. the scale of these functions cannot directly form a class library, but can only be encapsulated into functions and cannot be encapsulated into classes. For example, it includes some simple calling and instantiation methods of controllers, models, and views (different from Mysql classes because Mysql can be encapsulated into classes due to a series of operations)

Note that when writing these three methods, the controller method can have two parameters. The model and view correspond to only one parameter pitfall.

A series of Mysql operations can be encapsulated into a class because their operations correspond to an object, and simple calls and instantiation of controllers and models are implemented, if the view methods are integrated into one class, their objects cannot be unique, so they cannot integrate simple calling and instantiation controllers, models, and views into one class.

Libs folder

The library file of this micro-framework includes

  • Core Library

    Class files that can be called directly must be used for actual development, for example:

    1. database Class (note that the database class here is different from the database class. the database class stores a specific database operation method, one of the two parameters of the database class is the database type, that is, one of the database classes. In short, the database class is used to operate a specific database)

    2. view class (this class is used to initialize and call the object at the View layer. in this framework, the parameters that should be passed to the class initialization method are the configuration files of Smaty and Smarty)

  • Database

    It is used to store the code of a series of specific operations (such as mysql, mysqli, and pdo) in various databases.

  • Video Gallery

    The Smarty third-party class library is directly used here. Smarty is a view engine. the engine is embodied in the display method in Smarty. you can use the template file (or html file) compile and display it at the View layer

Include. list. php pc. php

Pc. php is a startup engine program. in this program, the database class and view class are initialized respectively, and the static variables of controller and method are initialized, the two static variables instantiate a controller and call the controller, which is equivalent to calling the controller in the first step of the MVC workflow and issuing instructions to it.

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.