Home  >  Article  >  Backend Development  >  Use native PHP to write a routing function like CodeIgniter_PHP Tutorial

Use native PHP to write a routing function like CodeIgniter_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:02711browse

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 made it more difficult for developers to see. 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.

Support default controller (index) and method (index):

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

define('MODULE_DIR', './classes/');
$APP_PATH= str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__);    
$SE_STRING=str_replace($APP_PATH, '', $_SERVER['REQUEST_URI']);    //计算出index.php后面的字段 index.php/controller/methon/id/3
$SE_STRING=trim($SE_STRING,'/');
//echo $SE_STRING.'<br>';
//这里需要对$SE_STRING进行过滤处理。
$ary_url=array(
    'controller'=>'index',
    'method'=>'index',
    'pramers'=>array()
    );
//var_dump($ary_url);
$ary_se=explode('/', $SE_STRING);
$se_count=count($ary_se);

//路由控制
if($se_count==1 and $ary_se[0]!='' ){
    $ary_url['controller']=$ary_se[0];

}else if($se_count>1){//计算后面的参数,key-value
    $ary_url['controller']=$ary_se[0];
    $ary_url['method']=$ary_se[1];
    if($se_count>2 and $se_count%2!=0){ //没有形成key-value形式
        die('参数错误');
    }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();    //实例化模块m

    if(!method_exists($obj_module, $method_name)){
        die('方法不存在');
    }else{
        if(is_callable(array($obj_module, $method_name))){    //该方法是否能被调用
            //var_dump($ary_url[pramers]);
            $get_return=$obj_module->$method_name($ary_url[pramers]);    //执行a方法,并把key-value参数的数组传过去
            if(!is_null($get_return)){ //返回值不为空
                var_dump($get_return);
            }
            
        }else{
            die('该方法不能被调用');
        }
        
    }
}
else
{
    die('模块文件不存在');
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752353.htmlTechArticleI 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