


Thinkphp routing rule usage examples and pseudo-static function implementation (apache rewriting)_PHP tutorial
//thinkphp 路由定义规则
$route = array(
'news/:action/:year\d/:month/:day'=>'news/read?year=:2&month=:3&day=:4',
'news/:action^delete|update|insert/:year\d/:month/:day'=>array( 'news/read?extra=:2&status=1','year=:2&month=:3&day=:4'),
);
$url = 'http://www.test.com/index.php/news/read/2012/2/21/extraparam/test.html';
//后缀名
$extension = 'html';
//可知: $_SERVER['PATH_INFO'] = 'news/read/2012/2/21/extraparam/test.html';
$regx = 'news/read/2012/2/21/extraparam/test.html';
//循环匹配路由规则
foreach($route as $key=>$value){
//如果匹配成功,则不继续匹配
if(parseUrlRule($key,$value,$regx,$extension))
break;
}
//运行结果: 打印$_GET
//Array
// (
// [actionName] => read
// [moduleName] => news
// [extra] => 2012
// [status] => 1
// [extraparam] => test
// [year] => 2012
// [month] => 2
// [day] => 21
// [finalUrl] => news/read?extra=2012&status=1&extraparam=test&year=2012&month=2&day=21
// )
// [Finished in 0.6s]
//相当于访问: http://www.test.com/news/read?extra=2012&status=1&extraparam=test&year=2012&month=2&day=21
//在部署时会把index.php隐藏,开启apache的重写模块
//重写规则 : RewriteRule ^(.+)$ /index.php/$1
//开启后,apache会自动把 http:/www.test.com/news/read/2012/2/21/extraparam/test.html转换为 http:/www.test.com/index.php/news/read/2012/2/21/extraparam/test.html
/**
* @$rule string 路由规则
* @$route string 规则映射的新地址
* @$regx string 地址栏pathinfo字符串
* @$extension stirng 伪静态拓展名
* return bool
*/
function parseUrlRule($rule,$route,$regx,$extension=null){
//去掉后缀名
!is_null($extension) && $regx = str_replace('.'.$extension,'',$regx);
//把路由规则和地址,分割到数组中,然后逐项匹配
$ruleArr = explode('/',$rule);
$regxArr = explode('/',$regx);
//$route以数组的格式传递,则取第一个
$url = is_array($route) ? $route[0] : $route;
$match =true;
//匹配检测
foreach($ruleArr as $key=>$value){
if(strpos($value,':')===0){
if(substr($value,-2)=='\\d' && !is_numeric($regxArr[$key])){
$match = false;
break;
}elseif(strpos($value,'^')){
$stripArr = explode('|',trim(strstr($value,'^'),'^'));
if(in_array($regxArr[$key],$stripArr)){
$match = false;
break;
}
}
//静态项不区分大小写
}elseif(strcasecmp($value, $regxArr[$key])!==0) {
$match = false;
break;
}
}
//匹配成功
if($match){
//把动态变量写入到数组$matches 中,同时去除静态匹配项
foreach($ruleArr as $key=>$value){
if(strpos($value,':')===0){
//获取动态变量,作为数组下标
if(substr($value,-2,1)=='\\')
$matchKey = substr($value,1,-2);
elseif($pos=strpos($value,'^'))
$matchKey =substr($value,1,$pos-1);
else
$matchKey = substr($value,1);
$matches[$matchKey] = array_shift($regxArr);
}else
array_shift($regxArr); //去除静态匹配项
}
//获取数组中的值,目的是配合子模式进行替换
$values = array_values($matches);
//正则匹配替换,正则需要用'e'作为修饰符
$url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url);
//解析url 格式: 分组/模块/操作?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// 分组/模块/操作?key1=value1&key2=value2
$arr = parse_url($url);
$paths = explode('/',$arr['path']);
parse_str($arr['query'],$queryArr);
}elseif(strpos($url,'/')!==false) //分组/模块/操作)
$paths = explode('/',$url);
else // key1=value1&key2=value2
parse_str($url,$queryArr);
//获取 分组 模块 操作
if(!empty($paths)){
$var['actionName'] = array_pop($paths);
$var['moduleName'] = array_pop($paths);
if(!empty($paths)){
$groupList = 'Home,Admin';
$temp = array_pop($paths);
if(in_array($temp,explode(',',$groupList)))
$var['groupName'] = $temp;
}
}
//合并的到GET数组中,方便全局调用
$_GET = array_merge($_GET,$var);
//合并参数
if(isset($queryArr))
$_GET = array_merge($_GET,$queryArr);
//匹配url中剩余的参数
preg_replace('/(\w+)\/([^,\/]+)/e','$tempArr[\'\\1\']=\'\\2\'',implode('/',$regxArr));
if(!empty($tempArr))
$_GET = array_merge($_GET,$tempArr);
//route是数组的话
if(is_array($route)){
$route[1]=preg_replace('/:(\d+)/e','$values[\\1-1]',$route[1]);
parse_str($route[1],$var);
$_GET = array_merge($_GET,$var);
strpos($url,'?')!==false ? $der ='&' : $der='?';
//最终写入到$_GET中的参数,包括三个部分
//1.地址栏剩余参数
//2.路由地址中的参数
//3.$route是数组时的第二个参数
if(!empty($tempArr))
$var = array_merge($tempArr,$var);
$url .=$der.http_build_query($var);
}
$_GET['finalUrl'] = $url;
//保证$_REQUEST 也能访问
$_REQUEST = array_merge($_REQUEST,$_GET);
//结果
print_r($_GET);
return true;
}
return $match;
}
//以下是正则路由代码:
$rule = '/news\/read\/(\d+)\/(\d+)\/(\d+)/';
$route ='news/read?year=:1&month=:2&day=:3';
$regx = 'news/read/2012/2/21/extraparam/test.html';
$extension = 'html';
parseUrlRuleRegx($rule,$route,$regx,$extension);
/**
* @$rule string Routing rule
* @$route string New address mapped by the rule
* @$regx string Address bar pathinfo string
* @$extension stirng Pseudo-static extension name
* return bool
*/
function parseUrlRuleRegx($rule,$route,$regx,$extension=null){
!is_null($extension) && $regx = str_replace( '.'.$extension,'',$regx);
$url = is_array($route) ? $route[0] : $route;
if(preg_match($rule,$regx,$matches )){
$url = preg_replace('/:(d+)/e','$matches[\1]',$url);
}else
return false;
// Parse url Format: Group/module/operation?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// Group/module/operation?key1=value1&key2= value2
$arr = parse_url($url);
$paths = explode('/',$arr['path']);
parse_str($arr['query'],$queryArr) ;
}elseif(strpos($url,'/')!==false) //Group/module/operation)
$paths = explode('/',$url);
else / / key1=value1&key2=value2
parse_str($url,$queryArr);
//Get the group module operation
if(!empty($paths)){
$var['actionName'] = array_pop($paths);
$var['moduleName '] = array_pop($paths);
if(!empty($paths)){
$groupList = 'Home,Admin';
$temp = array_pop($paths);
if (in_array($temp,explode(',',$groupList)))
$var['groupName'] = $temp;
}
}
//Merge into the GET array , convenient for global calls
$_GET = array_merge($_GET,$var);
if(isset($queryArr))
$_GET = array_merge($_GET,$queryArr);
//Match the remaining parameters
$regx = str_replace($matches[0],'',$regx);
preg_replace('/(w+)/([^,/]+)/ e','$tempArr['\1']='\2'',$regx);
if(!empty($tempArr)){
$_GET = array_merge($_GET,$tempArr) ;
strpos($url,'?')!==false ? $der='&':$der='?';
$url .=$der.http_build_query($tempArr);
}
if(is_array($route)){
$route[1] = preg_replace('/:(d+)/e','$matches[\1]',$route[1]) ;
parse_str($route[1],$var);
if(!empty($var)){
!empty($queryArr) && $var =array_merge($queryArr,$var) ;
$_GET= array_merge($_GET,$var);
}
strpos($url,'?')!==false ? $der='&':$der='?' ;
$url .=$der.http_build_query($var);
}
$_GET['finalUrl'] = $url;
print_r($_GET);
$_REQUEST = array_merge($_GET,$_REQUEST);
return true;
}
//Run results:
//Array
// (
// [actionName] => read
// [moduleName] => news
// [ year] => 2012
// [month] => 2
// [day] => 21
// [extraparam] => test
// [finalUrl] => news/read?year=2012&month=2&day=21&extraparam=test
// )
// [Finished in 0.1s]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.
