Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a simple MVC Framework with PHP

Shulou Source: shulou.com Published: 2022-05-31 17:32:27 09月14日 Update

This article mainly introduces how to use PHP to achieve a simple MVC framework related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this article on how to use PHP to achieve a simple MVC framework will have a harvest, let's take a look at it.

I. Preface

MVC, whose full name is Model View Controller, is a pattern for designing and creating web applications using model-view-controller, and is a design paradigm.

Where:

Model (model): the part of an application that deals with application data logic and is usually responsible for direct curd interaction with the database.

View (View): the part of the application that processes the display of data by rendering the processed data.

Controller (controller): the part of an application that handles user interaction and is usually used to handle the distribution of data logic and back to the view and controller accordingly.

Advantages:

Low coupling

High reusability

Fast deployment and low lifecycle cost

High maintainability

Disadvantages:

Increase the complexity of system structure and implementation

Overly tight connection between the view and the controller

2. Which frameworks use mvc architecture?

At present, mvc architecture has been used in the mainstream framework of php, such as:

ThinkPHP, Laravel and Yii all have the architecture of mvc.

III. Brief introduction of frame structure

Where:

App directory: used to store view controller models, etc.

Config directory: some common configuration files used to store the framework.

Core directory: used to store some base class files, such as controller base class, model base class, view base class, request request base class, db chain operation base class, etc.

Static directory: used to store some static resources, such as: pictures, html view files, js, cs files, etc.

Index.php: the entry file of the entire framework for distribution startup.

Fourth, the first step in the implementation of the framework (parsing routing)

Why is parsing routes the first step?

Only after successfully resolving the route, can you use MVC after accessing a method in a certain class, because parsing the route is also the most basic part of the framework implementation, and the automatic loading mechanism of PHP is necessary to resolve the route.

At present, the automatic loading mechanism of php can be divided into two mechanisms: composer automatic loading mechanism and manual automatic loading mechanism. the simple framework is completed by manual automatic loading mechanism.

4.1 write entry file

Index.php:

Tags: Controller control framework method view file route model code data mechanism processing application simplicity address application directory program logic function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia MariaDB Microsoft Shulou Technology