Simple Introduction to PHP programming single entry and multi-entry mode (case)

Source: Internet
Author: User
Tags learn php php programming

PHP Single-entry mode is now a popular large-scale Web application development model, such as the current more popular PHP development framework, zend,thinkphp,qeephp, and cakephp, and so they are all using the single entry mode. This article will be on what is a single entry model, the advantages of a single entry model to do a bit of research.

What is a single entry?

Before explaining what a single entry is, talk about the multiple entrances that correspond to them. Multiple portals run the corresponding function by accessing different PHP files. For example, when we first started to learn PHP, we did a project that would normally do the following:

index.php-Site Home

List.php?page=5-Content List page

INFO.PHP?ID=12-Content Detail Page

login.php-User Login page

For this project, this is actually a multi-entry.

Then the single-entry application means that all HTTP requests are processed in one file, such as the Content List page, the user login page, or the Content Detail page, which is processed by accessing the index.php file from the browser, this index.php The file is a single entry for this application.

How does PHP implement a single entry?

Quite simply, the general single entry program is accompanied by a specific parameter when accessing index.php. For example: Index.php?action=list can be defined as access to the Content List page, while index.php?action=info can be defined as access to the content Detail page, etc., the implementation code is as follows:
1
$action =$_get[' action ']== '? ' Index ': $_get[' action '];//takes the action parameter from the URL and sets a default ' index ' as the parameter if no action argument is supplied
2
Include (' files/'. $action. php ');//Call different code files according to the $action parameter to meet the different functions of a single portal implementation

This is the simplest single-entry mode program, but the real single-entry mode is a lot more complicated than this one. But as long as you know how to properly organize the various functions of the processing code and follow a certain step, you can easily solve the problem, below a background example to do a description:

For example, we need to do a news management backstage now. So first of all, a reasonable decomposition of the functionality of the application is to be made. For example, the background news column may contain "Add News", "Edit News", "Delete News" and other features. We can then combine this logically associative set of functions into a functional module called the "News management" module.
By finishing the application as described above, we get multiple function modules, each of which is composed of multiple functions (in fact, even if not a single-entry application, the collation of functions is a necessary step).

After finishing the function, we need to determine how to store the code for each function. Here I recommend two ways:

1, each function module a subdirectory, the directory of each file is a function of the implementation code.

The benefit of this approach is that each feature's code is isolated from each other, making it easy for multiple people to collaborate. The disadvantage is that sharing code and data between each feature is not so convenient. For example, all the functions in the News management module need a "Take out news column record" function, then the use of this multiple independent file organization, "Remove news column Record" Can only be written in another file, and then by the need for the function of the file include.

2. One file per module, each function in the module is written as a function or a class method.

The benefits don't have to be said much, it's easy to share code and data. The downside is that if several people change at the same time, conflict is easy. But with version control software and the diff-diff merge tool, conflicts can be easily resolved.

What are the advantages of a single-entry application for multiple portals?

All HTTP requests for a single-entry application are received and forwarded to the function code via index.php, so there is a lot of real work done in index.php (all the pages need to do and work the same). For example, a centralized security check, access statistics, and so on, if not a single portal, then developers must remember at the beginning of each file with security check code, of course, you might say that the multi-entry security check can be written to another file, and then include a bit. But actually for a relatively large application project, it is not a hassle to keep several include in the head in dozens of files.

Similar to security checks. In the portal, we can also make necessary checks and special character filtering for URL parameters and post, log logs, access statistics, and so on, which can be centrally handled tasks. As you can see, the work has been focused on index.php, which can ease the difficulty of maintaining other functional code.

Disadvantages of a single portal application?

There are two sides to everything, and a single portal application is no exception. Because all HTTP requests are Access index.php, the URL of the program looks less beautiful, especially for search engines. For example, the following URL:

Http://www.onesheng.cn/index.html

We know this URL is not easy to remember, and the search engine does not recognize it as a normal URL, of course, compared to the following URL:

Http://www.onesheng.cn/index.html

But this is not a big problem, you can use URL rewrite, pathinfo and so on can easily solve the problem.

OK, single entry mode is written so much, of course, to understand a single model, the best way is to try to use a single entry mode to write a small application out of deep experience.

Simple Introduction to PHP programming single entry and multi-entry mode (case)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.