Home >Backend Development >PHP Tutorial >Principles and Practice of PHP Routing Technology
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 a set of URL rules that you like, are search engine-friendly, and user-friendly
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.
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.
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>
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
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>
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>
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>
, 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>
<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>
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.