Home > Article > Backend Development > Detailed explanation of the steps to generate url according to routing rules using U method in thinkphp
This time I will bring you a detailed explanation of the steps to generate URLs according to routing rules using the U method in thinkphp. What are the precautions for using the U method in thinkphp to generate URLs according to routing rules? The following are Let’s take a look at practical cases.
is as follows:
//更改模块配置文件 'URL_ROUTER_ON' => true, 'URL_ROUTE_RULES'=>[]//编写路由优化
After tp turns on routing, using the U method will not generate the URL according to the routing rules. Generally, we You need to manually modify the template, remove the U method in it, and modify the link manually. If the program has already been written and the routing is added later, it will be too troublesome to modify the link.
I modified the U method today because I was bored. , let it generate URLs according to routing rules, and there is no need to modify the template one by one.
Add the following code to the /ThinkPHP/Common/functions.php file, directly search for if($suffix) in the U method, add the following code in front, the url generated by the u method is It is generated according to routing rules!
if(C('URL_ROUTE_RULES')){ foreach (C('URL_ROUTE_RULES') as $rule=>$real) { if(strpos($url, $real)!==false){ $url = str_replace($real, $rule, $url); preg_match("/\/(\w+)\.php\/(\w+)/", $url, $match); if(isset($match[1]) && isset($match[2]) && $match[1][0]==$match[2][0]){ $url = preg_replace("/\/(\w+)\.php/", '', $url); }elseif(strpos($url, 'index.php')!==false){ $url = str_replace("/index.php", '', $url); }else{ $url = str_replace(".php", '', $url); } preg_match_all("/(:\w+)/", $rule, $matches); foreach ((array)$matches[1] as $match) { $url = str_replace($match . '/', '', $url); $url = str_replace(substr($match, 1) . '/', '', $url); } } } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to reset the array to a continuous numeric index in PHP
Realize PHP to merge arrays and retain them What methods are there for key values?
The above is the detailed content of Detailed explanation of the steps to generate url according to routing rules using U method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!