In thinkphp, the U method generates a url according to routing rules. thinkphpurl
As follows:
// Change the module configuration file 'url _ ROUTER_ON '=> true, 'url _ ROUTE_RULES' => [] // write route optimization
After tp enables the routing, the url is not generated according to the routing rules by using the U method. Generally, we need to manually modify the template, remove the U method from the template, and manually modify the link, if you want to add a route only after you write a program, it is too troublesome to modify the link.
Today, when I was bored, I modified the U method to generate a url Based on the routing rules, and I didn't need to modify the templates one by one.
Add the following code to/ThinkPHP/Common/functions. php file, search for if ($ suffix) directly in the U method. Add the following code before. The url generated by the u method is generated according to the 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); } } } }
In the above thinkphp, the url generation method of the U method based on routing rules is all the content that I have shared with you. I hope you can give us a reference and also hope you can provide more support for the customer's house.