search
HomeBackend DevelopmentPHP TutorialPHP 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764156.htmlTechArticlephp single entry mode can be said to be a popular large-scale web application development mode, such as some of the more popular ones currently PHP development framework, zend, thinkphp, qeephp, cakephp, etc...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment