This article will give you one minute to learn how to use PHP to build your own web framework? (Sharing) has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
The WEB mentioned here refers to the PHP WEB program running under apache.
We must first understand the operating mechanism of PHP under apache and the life cycle of requests.
PHP is a scripting language. Its execution process starts from the file entry to the end of the file. It can contain or reference other files and is process-oriented. In the process, objects can be used to implement various required logical processing. You can use one or more objects to complete the required functions, and you can also tell an object what function you want to complete. This is an object-oriented development method and a common development method. Therefore, in the process-oriented operating mechanism, use the object-oriented development method.
The life cycle of each HTTP request also starts from the entrance until the end of the program. The variables in it will no longer exist. The variables of different HTTP requests are independent and do not affect each other. We can use global declarations, $GLOBALS global array variables, and static static variables to share data in the same HTTP request; use session to achieve session-level sharing; use cache to achieve site-wide data sharing. The global statement is generally used in methods and is used in process-oriented development. It is not usually used. $GLOBALS and static are often used, but they cannot be operated directly. Instead, they are managed in objects or special methods. For example, the commonly used singleton mode is saved using $GLOBALS and static.
In PHP programs, we will consider making the framework as simple, efficient, clear, and easy to use as possible, which is good for development and maintenance.
The basic program model uses the MVC model, which is layered and divided into modules. At the same time, a useful URL router is required to cooperate with MVC.
URL router: A very critical component that determines the organizational structure of the source code file and the clarity of the code. A good router can easily find the logical entry, reflecting the ease of use of the framework.
Model: Always use arrays. The main concern when using arrays is that the content of the array is unclear. In the project, the attributes refer to database fields, so the content of the array is relatively clear. Model operations are encapsulated using the data access layer DAO. In database access, it is also more efficient to convert directly into array form. For interactive data objects of other systems, there are generally interface document definitions. Regarding the Active Record technology in ORM, it is better not to use it or not.
View: I started using smarty, but in the performance report, the method executed by smarty took too much time. Later, I used tmd_tpl, which is just one file, easy to use and efficient. Easy to modify. In the view template, combine PHP syntax and supplement it with template variables. The idea of MVC is separation, which does not mean that PHP syntax cannot be used in View. If it is an API interface, the data can be directly converted into a specific format result and returned.
Controller: or action represents a behavior, a method, and an interface. Only one layer of controller is often not enough. It is generally divided into an interface layer, a business layer, a data access layer, and a communication layer. The interface is responsible for parameter verification, access permission control, calling specific services, and finally returning data or displaying pages, etc. It is best for all businesses to start with the interface layer. Before that, we should only do framework things. When we need to read a certain business implementation, we only need to start reading along the interface layer entrance. The business layer performs actual business functions. The business layer obtains data from the data access layer and performs business processing. The data access layer obtains data from the database or calling interface and can perform simple data conversion processing. If PHP is only used as the front end for data display, and the back end is executed by C/C/GO, etc., then you only need to encapsulate the business layer, request the data to the back end in the business layer, and then return it to the interface layer.
The above is the basic framework structure of the program, or the process structure of the business, which is usually the most important part of the system. But it is still far from being practical, and there are still many basic functions to be added, such as session processing, database access, log processing and other functions. These basic functions are generally independent of the framework and can be applied on different frameworks. Functional classes should not be too tightly coupled to the framework, and generally use combination methods. We encapsulate these basic functions into core classes in a way that is easy to use, and use single or multiple instances to call them, or further encapsulate the classes into global methods for easy use.
I think the simplest and easiest to use PHP website framework requires a route, a controller and a view template engine. Other functional modules can be added as needed.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to build your own web framework with PHP?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools