Overview of php single entry application

Source: Internet
Author: User
What is a single portal application? Before explaining what a single portal application is, let's take a look at traditional web applications. News. php displays the news list news_edit.php displays the news editing page. The two pages not only implement two functions, but also become two portals for applications. So what is the entrance? For example, <LINKhref = "http: // ww

What is a single portal application?

Before explaining what a single portal application is, let's take a look at traditional web applications.
News. php display news list
News_edit.php: Display the news editing page
These two pages not only implement two functions, but also become two portals for applications.

So what is the entrance?
For example, when everyone goes to WC, boys and girls enter one door. These two doors are the two entrances of WC.

The above example should be well understood. The concept of a single portal is easy to understand.
Now we are entering a public WC, regardless of whether men and women are entering from the outermost entrance. After paying the money, we will enter two separate doors. The outermost entrance is the single entrance of this WC.

Therefore, an application with a single portal actually uses a single file to process all HTTP requests. For example, whether the news list function or the news editing function, the index. php file is accessed from the browser. This index. php file is a single entry to this application.


How does index. php know which function is used by users?

It's easy. Just keep up with a specific parameter when accessing index. php. For example, index. php? Action = news is to display the news list, and index. php? Action = news_edit is the news editor.

In index. php, this effect can be achieved with only two lines of code.
$ Action = $ _ GET ['action'] = ''? 'Index': $ _ GET ['action'];
Include ('Files/'. $ action.'. Php ');
?>

In the above code, the first line is to retrieve the action parameter from the url. If the action parameter is not provided, a default 'index' is set as the parameter.
The second line of code is to call different code files according to the $ action parameter, so that a single entry can correspond to different functions.


Is the entry file of a single portal application complicated?

Some may think that the index. php of a single portal program is as complicated as a noodle, which is actually a misunderstanding.
For example, my current application portal file only contains the following lines:
Define ('app', realpath ('../libs/website '));
Define ('Lang ', 'gb2312 ');
Define ('debug', 1 );

Require ('../libs/flea1/basic. Php ');
Run ();
?>
Is it easy enough?

Of course, writing a long string of switch cases in index. php is definitely a poor implementation method. But this is purely a developer's own design and implementation, rather than a single portal application design.

Note: The switch case mentioned here does not mean that if a switch is used, it indicates "backward" and "rustic. Writing a bunch of switch cases in the index. php entry program is not conducive to program modification and maintenance, so it is a bad usage.


Design idea of a single portal application

When the web server (apache or iis) receives an http request, the request is parsed to determine which file to access. For example, the http://www.xxx.com/news.php resolution result is to require the web server to parse the news. php file and return the result to the browser. Now let's look at the index. php file of the single entry application, and we will find that index. php actually performs the second resolution based on the url parameter.

The program that completes this resolution is generally called Dispatcher (I do not know the exact translation of Chinese characters), which means forwarding different requests to different handlers for processing.

In a single entry application, index. php and the web server constitute a Dispatcher. The request processing program is determined based on the http request and url parameters.

After learning about the Dispatcher concept, we can find that the two lines of code mentioned above are actually the simplest implementation of Dispatcher:
$ Action = $ _ GET ['action'] = ''? 'Index': $ _ GET ['action'];
Include ('Files/'. $ action.'. Php ');
?>

Indeed, for a secure and robust application, Dispatcher is certainly not as simple as above. Before calling the actual code, various judgments and security checks will be added. For example, determine whether the function specified by the url can be accessed and whether the url contains invalid parameters.

When I see this, my friends will surely say: there is more than a single entry program, such as a dispatcher. What are the advantages of directly making a single file such as news. php and news_edit.php?

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.