Home >Backend Development >PHP Tutorial >PHP URL routing class example_PHP tutorial
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):
The specific class is as follows:
//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