大话URL映射之我见

Filed under: Other |
Posted on

1、大家来说说URL映射吧
一般url映射有两种方式,一种是通过mod_rewrite实现,这种网上教材很多我也不多说了。另外一种是在程序中模拟,比如类似zend Framework中的那种方式/index.php/controller/action/var1/value1/var2/value2/。这里方式其实最主要是通过一个统一的输入接口,然后对url进行解析,最后转发到相应的controller中的module。

我这里写了两个简单函数来模拟。
第一个函数主要是进行地址解析,把类似/index.php/controller/action/var1/value1/var2/value2/的地址解析出来,一般来说要解析成三部分:controller,module,params。

  1. <?php
  2. /**
  3.   *对url路由进行简单的解析,支持对/path/to/site/index.php/module/action/parm/value
  4.   * /path/to/site/index.php?/module/action/parm/value和
  5.   * /path/to/site/?/module/action/parm/value三种形式的处理
  6.   *@param:null
  7.   *@return:router array
  8.   */
  9. function url_router(){
  10.    $path=strip_tags($_SERVER['REQUEST_URI']);
  11.    $strpos=strpos($path,'.php');
  12.    if($strpos){
  13.        $path=substr($path,$strpos+4);
  14.    }else{
  15.        if(empty($_SERVER['QUERY_STRING'])){
  16.            $strpos=strpos($path,'?');
  17.            if($strpos){
  18.               $path=substr($path,$strpos+1);
  19.            }else{
  20.               $path='';
  21.            }
  22.        }else{
  23.            $path=$_SERVER['QUERY_STRING'];
  24.        }
  25.    }
  26.    //统一化$path的格式,如果$path的第一个字符为/则去掉
  27.    if($path[0]=='/'){
  28.        $path=substr($path,1);
  29.    }
  30.    //解析,并且路由
  31.    if(!empty($path)){
  32.        $path=explode('/',$path);
  33.        $router['controller']=$path[0];
  34.        $router['action']=(!empty($path[1]))?$path[1]:'index';
  35.        //print_r($path);
  36.        for($i=2;$i<sizeof($path);$i=$i+2){
  37.            $params[$path[$i]]=(isset($path[$i+1]))?$path[$i+1]:'';
  38.        }
  39.        $router['params']=$params;
  40.    }else{
  41.        //默认路由信息
  42.        $router['controller']='index';
  43.        $router['action']='index';
  44.        $router['params']=array();
  45.    }
  46.    return $router;
  47. }
  48. ?>

这里就完成主要的url解析功能,然后是转发映射,下面这个函数实现(注意这个函数的实现是结合了我自己的架构,所以你采用的话需要相应的修改,当然你的MVC如果类似zend Framework,那应该要该的不多。)

  1. <?php
  2. function url_dispatch($router,$app_path='/app/controllers/')
  3. {
  4.    require_once(SERVER_PATH.'/libs/controller.class.php');
  5.    $controller=$router['controller'].'Controller';
  6.    //echo SERVER_PATH.$app_path.$controller.'.class.php';
  7.    if(!file_exists(SERVER_PATH.$app_path.$controller.'.class.php'))die('缺少必要的类!');
  8.    require_once(SERVER_PATH.$app_path.$controller.'.class.php');
  9.    $controller=new $controller();
  10.    $controller->_setParam($router['params']);
  11.    $controller->{$router['action'].'Action'}();
  12.    return true;
  13. }
  14. ?>
Tags : , ,   阅读次数: 22

3 Responses to “大话URL映射之我见”

  1. wow gold

    I play wow in 3 years and i know some wow gold,I love wow gold.

  2. hqbhmt

    vcmyzuaghcjwcnbrvlrdkahmtkaeep

  3. wow gold

    We have been an ebay power seller and paypal confirmed seller of wow gold for years.

Leave a Reply

You must be logged in to post a comment.