PHP single entry mode explained in detail_PHP tutorial
The PHP single entry mode can be said to be a popular large-scale web application development mode. For example, some of the more popular PHP development frameworks, zend, thinkphp, qeephp, and cakephp, etc. all use a single entry mode. Entry mode. This article will do some research on what the single entry mode is and what the advantages and disadvantages of the single entry mode are.
What is a single entrance?
Before explaining what a single entrance is, let’s first talk about the corresponding multiple entrances. Multi-entry means running corresponding functions by accessing different php files. For example, when we first started learning PHP, we usually did the following when doing a project:
index.php – Home page
list.php?page=5 - Content list page
info.php?id=12 - Content details page
login.php – User login page
For this project, this is actually a multiple entrance.
Then a single-entry application means using one file to process all HTTP requests. For example, whether it is a content list page, a user login page or a content detail page, it is processed by accessing the index.php file from the browser. , the index.php file here is the single entry point for this application.
How does php implement a single entrance?
It’s very simple. Generally, single-entry programs come with a specific parameter when accessing index.php. For example: index.php?action=list can be defined to access the content list page, and index.php?action=info can be defined to access the content detail page, etc. The specific implementation code is as follows:
$action=$_GET['action']==''?'index':$_GET['action'];//从url中取出action参数,如果没有提供action参数,就设置一个默认的'index'作为参数 include('files/'.$action.'.php');//根据$action参数调用不同的代码文件,从而满足单一入口实现对应的不同的功能
The above implements the simplest single entry mode program. Of course, the real single entry mode will be much more complicated than this. But as long as you know how to reasonably organize the processing code of each function and follow certain steps, you can easily solve this problem. Here is a background example to illustrate:
For example, we are now going to build a backend for news management. So first, we need to make a reasonable decomposition of the application's functions. For example, the news column in the background may include multiple functions such as "add news", "edit news", "delete news", etc. At this time, we can combine this set of logically related functions into a functional module, called the "news management" module.
After sorting out the application's functions according to the above method, we will get multiple functional modules, and each module is composed of multiple functions (in fact, even if it is not a single-entry application, function sorting is a necessary step) .
After organizing the functions, we need to determine how to store the code for each function. Here I recommend two methods:
1. Each functional module has a subdirectory, and each file in the directory is the implementation code of a function.
The advantage of this method is that the code of each function is isolated from each other, which is very convenient for multi-person collaboration. The disadvantage is that it is not so convenient to share code and data between each function. For example, all the functions in the news management module require a function of "retrieving news column records", so using this organization method of multiple independent files, "retrieving news column records" can only be written in another file, and then by Include the files that require this function.
2. Each module has one file, and each function in the module is written as a function or a class method.
Needless to say the benefits, it is very convenient to share code and data. The disadvantage is that if several people make changes at the same time, conflicts may easily occur. However, conflicts can be easily resolved with the help of version control software and difference comparison and merge tools.
What are the advantages of a single portal application versus multiple portals?
All http requests of the single-entry application are received through index.php and forwarded to the function code, so a lot of actual work can be done in index.php (all pages need to do the same thing Work). For example, conduct centralized security checks, access statistics, etc. If it is not a single entry, then the developer must remember to add the security check code at the beginning of each file. Of course, you may say, multi-entry security checks You can write it to another file and then include it. But actually for a relatively large application project, it is not a worry-free thing to keep the several includes in the header consistent in dozens of files.
Similar to security check. In the portal, we can also perform necessary inspections and special character filtering on URL parameters and posts, log recording, access statistics, and other tasks that can be processed centrally. It can be seen that since these tasks are concentrated in index.php, it can reduce the difficulty of maintaining other functional codes.
Disadvantages of Single Entry Application?
Everything has two sides, and single-entry applications are no exception. Since all http requests access index.php, the URL of the program does not look so beautiful, especially not friendly to search engines. For example, the following url:
http://www.phpernote.com/index.php?controller=posts&action=index
We know that this kind of URl is not easy to remember, and search engines do not recognize it as a normal URL. Of course, this is compared to the following URl:
http://www.phpernote.com/index.php/posts/index/
However, this is not a big problem. You can easily solve this problem by using url rewriting, PATHINFO, etc.
OK, that’s all I’ve written about the single entry mode. Of course, if you want to deeply understand the single entry mode, the best way is to try to write a small application using the single entry mode to have a deep understanding of it.
Articles you may be interested in
- Using php functions in smarty templates and how to use multiple functions for one variable in smarty templates
- How to delete an array in php The first element and the last element
- How to round to precise decimal places and round in PHP
- How to determine whether a constant has been defined in php
- How to round php in html brLine breaks are converted into line breaks in text input
- Analyze the reasons for the continuous decrease in Baidu index volume and how to increase Baidu index volume
- php obtains the pm2.5 data interface development of a certain place (pm2.5 php api)
- Mysql query strings with single quotes and insert strings with single quotes need to pay attention to issues

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
