PHP Single Portal Application overview

Source: Internet
Author: User
Tags switch case

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 Show News List
News_edit.php Show News Edit Page
These two pages have not only implemented two functions respectively, but also become the two portals of the application.

What's the entrance?
For example, everyone on the WC, are boys into a door, girls into a door. These two doors are the two entrances to the WC.

Hehe, the above example should be well understood. The concept of a single entry is easy to understand, if you change it a little.
Now we are entering a public WC, where men and women enter from the outermost entrance and pay two doors respectively. The outermost entrance is the single inlet of the WC.

So a single-entry application is essentially a file that handles all HTTP requests. For example, the index.php file is accessed from the browser, whether it is a news list feature or a news editing feature. This index.php file is a single entry for this application.


How does index.php know which feature the user is using?

Very simply, we can follow a specific parameter when we visit index.php. Index.php?action=news, for example, shows a list of news, while Index.php?action=news_edit is a news editor.

In index.php, this effect can be achieved with just two lines of code.
<?php
$action = $_get[' action '] = = '? ' Index ': $_get[' action ';
Include (' files/'. $action. '. php ');
?>

In the above code, the first line is to remove 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 invoke different code files according to the $action parameters to achieve the effect of a single entry corresponding to different functions.


The portal file for a single portal application is complex?

Some friends may think that the index.php of a single entry program will be as complex as noodles, but it is a misunderstanding.
For example, my application portal file now has only the following lines:
<?php
Define (' APP ', Realpath ('. /libs/website '));
Define (' LANG ', ' gb2312 ');
Define (' DEBUG ', 1);

Require ('.. /libs/flea1/basic.php ');
Run ();
?>
Simple enough, huh?

Of course, writing a long list of switch case in index.php is definitely a bad way to implement it. But this is purely a developer's own design and implementation problem, rather than a single-entry application of this design idea.

Note: The switch case mentioned here is not to say that the switch is used to represent "backward", "rustic" and so on. Just say that in the index.php this entry program is written in a bunch of switch case is not conducive to program modification and maintenance, so is a bad usage.


Design ideas for single entry applications

When the Web server (Apache or IIS) receives an HTTP request, it resolves the request and determines which file to access. For example, the parsing result of http://www.xxx.com/news.php is to ask the Web server to parse the news.php file and return the results to the browser. Now look at the index.php file for the single-entry application, and you'll find that index.php actually has a second parse based on the URL parameter.

The process of completing this parsing is generally called Dispatcher (I do not know the exact translation of Chinese), presumably meaning that different requests are forwarded to different handlers for processing.

In a single-entry application, index.php and the Web server together form a Dispatcher that determines the requested handler based on the HTTP request and URL parameters.

Having understood the concept of Dispatcher, we can see that the two lines of code mentioned earlier are actually the simplest Dispatcher implementations:
<?php
$action = $_get[' action '] = = '? ' Index ': $_get[' action ';
Include (' files/'. $action. '. php ');
?>

Admittedly, for a secure, robust application, Dispatcher is certainly not as simple as it is. Before invoking the actual code, a variety of judgments, security checks, etc. are added. For example, determine whether the function specified by the URL is accessible and the URL contains invalid parameters.

See here, friends will certainly say: the single entrance procedure is more such a dispatcher, and I directly into the news.php, news_edit.php and other individual files compared to what benefits ah?

From: http://www.php100.com/html/webkaifa/PHP/PHP/2009/0819/3199.html

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.