Php single entry mode-PHP Tutorial

Source: Internet
Author: User
Tags smarty template
Php single entry mode. The single entry mode of php is a popular large-scale web application development mode, such as some popular php development frameworks, zend, thinkphp, and qeephp, there is also a single php entry mode such as cakephp, which is now a popular large-scale web application development mode, such as some popular php development frameworks, zend, thinkphp, and qeephp, in addition, cakephp and others all adopt the single entry mode. This article will focus on the advantages and disadvantages of the single entry mode and the single entry mode.

What is a single portal?

Before explaining what a single portal is, let's talk about the corresponding multiple portals. Multiple portals are used to access different php files to run corresponding functions. For example, when we first started learning php, we usually do the following for a project:

Index. php-website homepage

List. php? Page = 5-content list page
 
Info. php? Id = 12-content details page
 
Login. php-user logon page

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

The application with a single portal uses a single file to process all HTTP requests. for example, whether it is a content list page, a user logon page, or a content details page, index is accessed through a browser. php file for processing, here the index. the php file is a single entry to this application.

How does php achieve a single portal?

It is very simple. generally, a single entry program comes with a specific parameter when accessing index. php. For example, index. php? Action = list can be defined as the access content list page, and index. php? Action = info can be defined as the detailed page of access content. the specific implementation code is as follows:

$ Action = $ _ GET ['action'] = ''? 'Index': $ _ GET ['action']; // retrieves the action parameter from the url. if the action parameter is not provided, set a default 'index' as the parameter include ('Files /'. $ action. '. php '); // call different code files based on the $ action parameter to implement different functions for a single entry

The above implements the simplest single-entry mode Program. of course, the real single-entry mode is much more complicated than this one. However, as long as you understand how to properly organize the code for each function and follow certain steps, you can easily solve this problem. The following is an example of the background:

For example, we need to create a background for news management. First, make a reasonable decomposition of the functions of the application. For example, the background news section may contain "add news", "edit news", and "delete News. In this case, we can combine the logic-related functions of this group into a function module called the "news management" module.
After finishing the functions of the application 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 portal application, function sorting is also a required step ).

After finishing the functions, we need to determine how to store the code of each function. Here I recommend two methods:

1. each function module has a sub-directory, and each file in the directory is a function implementation code.

The advantage of this method is that the code of each function is isolated from each other, so that it is easy for multiple people to collaborate. The disadvantage is that sharing code and data between each function is not that convenient. For example, all the functions in the news management module require the function of "retrieving news column records", so the organization of multiple independent files is adopted, "retrieve the news record" can only be written in another file, and then included in the file that requires this function.

2. Each module has a file, and each function in the module is written as a function or a class method.

It is easy to share code and data. The disadvantage is that, if a few people change at the same time, conflicts may occur. However, conflicts are easily solved by using version control software and comparison tools.

What are the advantages of a single portal application corresponding to multiple portals?

All http requests of a single portal application are sent through index. php receives and forwards the code to the function. php can complete a lot of practical work (all pages need to do the same work ). For example, for centralized security checks and access statistics, if it is not a single entry, developers must remember to add the security check code at the beginning of each file. of course, you may say, you can write the multi-entry security check to another file, and then include it. However, for a relatively large application project, it is not a worry-free task to keep several include entries in the header consistent in dozens of files.

Similar to security checks. In the portal, we can also perform necessary checks and special character filtering on url parameters and post, record logs, access statistics, and other tasks that can be centrally processed. In this way, we can see that because these tasks are concentrated in index. php, we can reduce the difficulty of maintaining other functional code.

What are the disadvantages of a single portal application?

Everything has two sides, and a single portal application is no exception. Because all http requests access index. php, the url of the program does not look so beautiful, especially for search engines. For example, the following url:

Http://www.phpernote.com/index.php? Controller = posts & action = index

We know that 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.phpernote.com/index.php/posts/index/

However, this is not a big problem. you can use url rewriting, PATHINFO, and other methods to easily solve this problem.

Okay, there are so many entries in the single portal mode. of course, if you want to deeply understand the single portal mode, the best way is to try to write a small application in the single portal mode.

Articles you may be interested in
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • How does php delete the first and last elements of an array?
  • PHP rounding precision decimal places and rounding methods
  • How does php determine whether a constant has been defined?
  • How does php convert the br line breaks in html to line breaks in text input?
  • Analyze the reasons why Baidu index volume is decreasing and how to increase Baidu Index Volume
  • Php SDK for retrieving pm2.5 data from a certain place (pm2.5 php api)
  • Note the following when querying strings with single quotes and inserting strings with single quotes in Mysql:

Wait...

Related Article

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.