php自定义路由首页代码:

<?php
echo '<pre>';
//var_dump($_SERVER);
$route_ok = false;

/*设置register_argc_argv = On(默认是Off),重启服务
$_SERVER[‘argv’]才会有效果*/

if(isset($_SERVER['argv']) && !isset($_SERVER['REQUEST_URI'])){
    $url = urldecode($_SERVER['argv'][1]);
}else{
    $url = urldecode($_SERVER['REQUEST_URI']);
}

echo $url;
echo '<br>=====<br>';
$open_urlroute=true; //伪静态开关
if($open_urlroute){
$open_url_route = include ('route.php');
//http://www.b.com/luyou/
$urls = '';//   /luyou/
foreach($open_url_route as $k=>$v){
    if($v!='' && $v[0]!='' && $v[1]!=''){
        echo $route_ok = preg_match_all($v[0],$url,$matches);//匹配到为1
        echo '<br>======route_ok=====<br>';
        echo $urls = $v[1];//   test/id/1
        echo '<br>=====urls=====<br>';
        echo $method = strtoupper($v[2]);//GET POST方式
        echo '<br>=====<br>';
        if($route_ok){
            break;
        }
    }
}


if($route_ok){
    //print_r($matches);
    foreach($matches as $k=>$v){
        $urls = str_replace('$'.$k,$v[0],$urls);
    }
    echo $url = $urls;

}

//http://www.b.com/luyou/index.php?id=aa&b=4
$position = strpos($url,'?');
if($position!==false){
    echo '  ||| '.$param = substr($url,$position+1);
    parse_str($param,$_GET);//将“id=aa&b=4”转成数组放在$_GET里面

    var_dump($_GET);
        /*
        array(2) {
                ["id"]=>string(2) "aa"
                ["b"]=>string(1) "4"
        }
        */
}

}else{
    $open_url_route = [];

}

route.php代码:

return [
	/**
		['正则url','系统内真实链接','传输方式POST/GET,或者为空,则表示POST/GET都接收']
		如果有多条匹配,默认第一条有效
		demo:
		['/\/base\/([0-9]+)\.html$/','Home/test/id/$1','GET'],
		['/\/xbase\/([0-9]+)\/(\w+)\.html$/','Home/test/id/$1/sq/$2','POST'],
		['/\/test_([0-9]+)\.html$/','Home/test/id/$1','GET'],
		['/\/abc\.html$/','/shangpin.html',''],
	**/
	//以下规则不可删除,否则会报错!
	//http://demo.jizhicms.cn/Home/screen/molds/product/categories/3
    ['/^\/luyou\/(.*)/','test/id/$1','POST'],
	['/^\/screen-(\w+)-([0-9]+)-(.*)/','Screen/index/molds/$1/tid/$2/jz_screen/$3',''],
	['/^\/searchAll(.*)/','Home/searchAll','GET'],
	['/^\/search(.*)/','Home/search','GET'],
	
];

php自定义路由结束

发表评论 已发布 0

还可以输入 800 个字
 
 
打印