How thinkphp generates URLs according to routing rules

php中世界最好的语言
Release: 2023-03-22 09:00:01
Original
1855 people have browsed it

This time I will show you how thinkphp generates URLs according to routing rules. What are the precautions for thinkphp to generate URLs according to routing rules? The following is a practical case. Let’s take a look. one time.

is as follows:

//更改模块配置文件
'URL_ROUTER_ON'   => true, 
'URL_ROUTE_RULES'=>[]//编写路由优化
Copy after login

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 manually modify the link. 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);
   }
  } 
 } 
}
Copy after login

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:

Post cannot obtain form data

##Forced downloading of QR code images

How to operate format files in php

The above is the detailed content of How thinkphp generates URLs according to routing rules. For more information, please follow other related articles on the PHP Chinese website!

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!