PHP single entry mode explained in detail_PHP tutorial

WBOY
Release: 2016-07-13 10:31:03
Original
1544 people have browsed it

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参数调用不同的代码文件,从而满足单一入口实现对应的不同的功能
Copy after login

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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template