Principles and Practice of PHP Routing Technology

WBOY
Release: 2016-07-30 13:30:59
Original
1060 people have browsed it

0x00 Routing Implementation Principle

Users access the backend through the specified URL paradigm. After processing by the URL routing processing class, it is forwarded to the logic processing class, which returns the request results to the user.

Agree on URL paradigms and rules

Agree on a set of URL rules that you like, are search engine-friendly, and user-friendly

URL processing class (i.e., the core of routing implementation)

parse and process the URL requested by the user, Obtain the class, method, and Query parameters requested by the user, and forward the request to the logical processing class.

Logic processing class

handles the real business logic of the website.

0x01 URL paradigm convention

Currently, there are two popular URL formats, one is the normal mode and the other is the pathinfo mode.

Normal mode

In the ThinkPHP framework, the default URL format is normal mode. The normal mode URL is as follows:

<code>index.php?m=home&c=user&<span>a</span>=login&v=<span>value</span></code>
Copy after login

where the value of the m parameter is the module name, and the value of the c parameter is the controller name, the value of the a parameter is the method name, and the subsequent parameters are other GET request parameters to be received in the method

pathinfo mode

In the CodeIgniter framework, the default URL format is pathinfo The pattern is as follows:

<code><span>index</span>.php/controller/<span><span>method</span>/<span>prarme1</span>/<span>value1</span></span></code>
Copy after login

The meaning of this block has also been clearly marked. After method, it is the GET parameter received by the method, and the format is name/value

0x02 URL routing processing class ( Core)

Here we choose the simplest ordinary single module mode for demonstration, just to illustrate the simple principle, as follows:

<code>index.php?c=user&<span>a</span>=login&v=<span>value</span></code>
Copy after login

We agree that the parameter c is the controller name, and the parameter a is the method name, and then They are all GET parameters

<code><span><span><?php</span><span>include</span><span>'index.class.php'</span>;
<span>include</span><span>'user.class.php'</span>;
<span>// 对用户请求URL进行处理</span><span>$query</span> = <span>$_GET</span>;
<span>$controller</span> = <span>isset</span>(<span>$query</span>[<span>'c'</span>]) ? <span>$query</span>[<span>'c'</span>] : <span>'indexController'</span>;
<span>$action</span> = <span>isset</span>(<span>$query</span>[<span>'a'</span>]) ? <span>$query</span>[<span>'a'</span>] : <span>'index'</span>;
<span>if</span> (class_exists(<span>$controller</span>)) {
    <span>if</span> (method_exists(<span>$controller</span>, <span>$action</span>)) {

        <span>unset</span>(<span>$_GET</span>[<span>'c'</span>]);
        <span>unset</span>(<span>$_GET</span>[<span>'a'</span>]);
        <span>// 实例化用户请求类并调用方法</span>
        (<span>new</span><span>$controller</span>())-><span>$action</span>();

    } <span>else</span> {
        <span>echo</span><span>'控制器'</span> . <span>$controller</span> . <span>'中不存在方法'</span> . <span>$action</span>;
    }
} <span>else</span> {
    <span>echo</span><span>'不存在控制器'</span> . <span>$controller</span>;
}</span></span></code>
Copy after login

, among which unset() removes two get parameters, just to have other effects on the actually called method.

0x03 Logic processing class

The logic processing class is the final business logic, which is the code fragment that actually responds to user requests. The following is just a simple example:

<code><span>/* index.class.php 文件源码 */</span><span><?php</span><span><span>class</span><span>indexController</span> {</span><span>public</span><span><span>function</span><span>index</span><span>()</span>{</span>
        var_dump(<span>$_GET</span>);
    }
}</code>
Copy after login
<code><span>/* user.class.php 文件源码 */</span><span><?php</span><span><span>class</span><span>user</span> {</span><span>public</span><span><span>function</span><span>index</span><span>()</span> {</span><span>echo</span><span>'这里是User控制器'</span>;
    }

    <span>public</span><span><span>function</span><span>login</span><span>()</span> {</span>
        var_dump(<span>$_GET</span>);
    }
}</code>
Copy after login

0x04 End

This is just the principle of the simplest PHP routing technology. In fact, to truly develop routing for a project or framework, you may need to be able to be compatible with many complex situations and need to be able to handle various situations. All should be considered.

Original author: I am Erliang
Original link: http://www.2liang.me/?p=230
Reprints must be marked in the text and retain the original link, author and other information.

The above has introduced the principles and practices of PHP routing technology, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!