如何在PHP项目中实现一个url路由功能

  介绍

这篇文章将为大家详细讲解有关如何在PHP项目中实现一个url路由功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<强>什么是PHP的路由机制

1,路由机制就是把某一个特定形式的url结构中提炼出来系统对应的参数。举个例子,如:http://main.test.com/article/1 其中://1条,→? _m=article& id=1 .

2,然后将拥有对应参数的URL转换成特定形式的URL结构,是上面的过程的逆向过程。

如果一个页面的内容呈现,需要根据URL上传递的参数来进行渲染。很多时候可能是这样子写:xxx.com/xx?c=x& m=x& t=. .而我们看到的URL往往是这样子的(以新浪微游戏的咖啡恋人为例)game.weibo.com/ilovecoffee…。这种URL设计看上去比前一种更好一点:)

如果我们访问一下不存在的游戏应用,例如game.weibo.com/ilovecoffee222,则会输出如下的错误提示:如何在PHP项目中实现一个URL路由功能“> <br/> </p> <p> game.weibo.com后面匹配到的项,指向了某个PHP页面,然后根据参数获取要访问的游戏应用标识,后数据库或者缓存里查询该应用标识,如果不存在则输出错误提示,如果应用存在则加载游戏应用链接地址。</p> <p>,现在写一个PHP例子,假设我的ip为192.168.0.33,我加了一层名为路由器的路径,之后跟随的是“/模块名/方法名/参数1的键/参数1的值/!?/p> <p>类似这样的地址:</p> <p> 192.168.0.33/路由器/Hello/路由器/a/b/c/d/abc/index . html ? id=3, URL=http:………………</p> <p>也就是要调用Ha这个模块中路由器的方法,并传入URL后面的参数/a/b/c/d/索引…………。</p> <p>,第一步,首先要在服务器的配置上对/路由器/路径进行拦截<img src=

第二步,路由分发器的实现(index . php)

& lt; ! Doctype  html>   ,& lt; html>   ,& lt; head>   ,& lt; title>路由测试~ ~ & lt;/title>   ,& lt; meta  http-equiv=癱ontent-type",内容=皌ext/html;, charset=utf-8",/比;   ,& lt;/head>   & lt; body>   php & lt; ?   作用(“亚洲/Shanghai");   ,定义(“MODULE_DIR",,“. ./类/?;=,美元_DocumentPath  $ _SERVER [& # 39; document_root # 39;];   ,_FilePath 美元;=,__FILE__;=,美元_RequestUri  $ _SERVER [& # 39; REQUEST_URI& # 39;];,   ,_AppPath 美元;=,str_replace (_DocumentPath美元,& # 39;& # 39;,,_FilePath美元);,,//==祝辞\路由器\ index . php   美元,美元_UrlPath =, _RequestUri;,,//==祝辞/路由器/hello/路由器/a/b/c/d/abc/index . html ? id=3, url=http:   ,_AppPathArr 美元;=,爆炸(DIRECTORY_SEPARATOR, _AppPath美元);   ,/* *   *大敌;http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3& url=http:   *大敌;   ,*/hello/路由器/a/b/c/d/abc/index . html ? id=3, url=http:   ,*/,for ($小姐:=,0;,美元小姐:& lt;, count ($ _AppPathArr);,我+ +美元),{   ,,p 美元;=,_AppPathArr美元($ i);   美元,才能if  (p), {   ,,,_UrlPath 美元;=,preg_replace (& # 39;/^ \/& # 39; $ - - # 39; \//& # 39;,, & # 39;/& # 39;,, _UrlPath美元,1);   ,,}   ,}   ,   ,_UrlPath 美元;=,preg_replace (& # 39;/^ \//& # 39;,, & # 39; & # 39;,, _UrlPath美元,1);   ,   ,_AppPathArr 美元;=,爆炸(“/?,_UrlPath美元);   ,_AppPathArr_Count 美元;=,count ($ _AppPathArr);   ,   ,arr_url 美元;=,阵列(   & # 39;才能控制器# 39;,=祝辞,& # 39;指数# 39;   ,,& # 39;方法# 39;,=祝辞,& # 39;指数# 39;   ,,& # 39;parms& # 39;,=祝辞,数组()   ,);   ,   ,$ arr_url[& # 39;控制器# 39;],=,_AppPathArr美元[0];   ,$ arr_url[& # 39;方法# 39;],=,_AppPathArr美元[1];   ,   ,if  (_AppPathArr_Count 美元;祝辞,2,以及_AppPathArr_Count 美元;%,2,!=,0),{   ,,死(& # 39;参数错误& # 39;);   ,}else  {   ,,for ($小姐:=,2,,小姐:美元& lt;, _AppPathArr_Count;美元,美元小姐:+=,2),{   ,,,,arr_temp_hash 美元;=,数组(函数($ _AppPathArr[我]美元)=祝辞_AppPathArr美元[小姐:美元+,1]);   ,,,,美元arr_url [& # 39; parms& # 39;],=, array_merge (arr_url美元[& # 39;parms& # 39;],, arr_temp_hash美元);   ,,}   ,}   ,   美元,美元module_name =, arr_url[& # 39;控制器# 39;];   ,module_file 美元;=MODULE_DIR。美元module_name强生# 39;.class.php& # 39;;   美元,美元method_name =, arr_url[& # 39;方法# 39;];   ,   ,if  (file_exists (module_file美元)),{   ,,include  module_file美元;   ,,   ,,obj_module 美元;=,new 美元module_name ();   ,,   ,,if  (! method_exists (obj_module美元,,method_name)), {   ,,,,死(“要调用的方法不存在“);   ,,},{else    ,,,,if  (is_callable(数组(obj_module美元,,method_name))), {   ,,,,,,obj_module 美元;→,method_name美元($ module_name, arr_url美元[& # 39;parms& # 39;]);   ,,,,,,   ,,,,,,obj_module 美元;→,printResult ();   ,,,,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在PHP项目中实现一个url路由功能