Home >Backend Development >PHP Tutorial >PHP URL routing class example_PHP tutorial

PHP URL routing class example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:18917browse

I wrote an API for a mobile application some time ago. I always used the query_string address, and all actions were distinguished based on an act parameter. This seemed to be more troublesome for developers. I originally wanted to rewrite it as "?c=controller&m=method&type=3&id=1", using the m parameter to load the file and instantiate it. Later, I saw that the sina weibo api routed the address. Also decided to follow suit on address routing. Originally, the CI framework had its own routing effect, but because I was considering writing an API, I wanted to write it more purely.
Supports default controller (index) and method (index):

Copy code The code is as follows:

index. php
index.php/controller
index.php/controller/method
index.php/controller/method/prarme1/value1
index.php/controller/method/param1/value1/param2/ value2.....

The specific class is as follows:

Copy the code The code is as follows:

$APP_PATH= str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__);
$SE_STRING=str_replace($APP_PATH, '', $_SERVER['REQUEST_URI']) ; //Calculate the field index.php/controller/methon/id/3 after index.php
$SE_STRING=trim($SE_STRING,'/');
//echo $SE_STRING.'< br>';
//$SE_STRING needs to be filtered here.
$ary_url=array(
'controller'=>'index',
'method'=>'index',
'pramers'=>array()
) ;
//var_dump($ary_url);
$ary_se=explode('/', $SE_STRING);
$se_count=count($ary_se);

//Routing control
if($se_count==1 and $ary_se[0]!='' ){
$ary_url['controller']=$ary_se[0];

}else if($se_count>1){//Calculate the following parameters, key-value
$ary_url['controller']=$ary_se[0];
$ary_url['method'] =$ary_se[1];
if($se_count>2 and $se_count%2!=0){ //The key-value form is not formed
die('Parameter error');
}else {
for($i=2;$i < $se_count;$i=$i+2){
$ary_kv_hash=array(strtolower($ary_se[$i])=>$ary_se[ $i+1]);
$ary_url[pramers]=array_merge($ary_url[pramers],$ary_kv_hash);
}
}
}


$module_name=$ary_url['controller'];
$module_file=MODULE_DIR.$module_name.'.class.php';
//echo $module_file;
$method_name= $ary_url['method'];
if(file_exists($module_file)){
include($module_file);
$obj_module=new $module_name(); //Instantiate module m

if(!method_exists($obj_module, $method_name)){
die('Method does not exist');
}else{
if(is_callable(array($obj_module, $method_name)) ){ //Whether this method can be called
//var_dump($ary_url[pramers]);
, and pass the array of key-value parameters
                                                                                                                                                                                                           }else{
                                                                      ');
}
?>






http://www.bkjia.com/PHPjc/825120.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/825120.htmlTechArticle

I wrote an API about mobile applications some time ago. I always used the query_string address, and it was based on a The act parameter is used to distinguish all actions, which makes developers look better than...
Statement:
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