注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

php路由实现rewrite改写url  

2012-04-18 11:55:25|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源:http://phpd.cn/archives/558

最近着手写一个网站,从框架到应用全部重新开发,很多代码属于重造轮子,但主要的目的就是练手,因为发现最近思维有些固化了,是得好好从头到底写个项目了!

以前写的框架中路由功能非常有限,只是实现controller与action的选择,代码很简陋,没做过滤,安全性也有问题,所以就重写了一个路由。

框架出错提示

主要功能:

  1. controller与action的选择
  2. php正则rewrite美化url
  3. 参数过滤
  4. PATH_INFO与REQUEST_URI自动选择

代码如下

class router {
  
    static public $controller = '';
    static public $action     = '';
    static public $params     = array();
  
    static public function get() {
        $config = array(
                        'controller_default'    => 'index',
                        'action_defalut'        => 'index',
                        'controller_delimiter'  => '/',
                        'action_delimiter'      => '/',
                        'params_delimiter'      => '/',
                        'params_kv_delimiter'   => '/',
                        'extension_default'     => '.html',
                    );
        if (false != $routerConfig = nash::config('main', 'router')) $config = array_merge($config, $routerConfig);
  
        //判断是否是GET形式
        if (isset($_GET['c']) || isset($_GET['a'])) {
            self::$controller = isset($_GET['c']) ? self::_filter($_GET['c']) : $config['controller_default'];
            self::$action = isset($_GET['a']) ? self::_filter($_GET['a']) : $config['action_defalut'];
            foreach ($_GET as $key => $value) {
                if (!in_array($key, array('c', 'a'))) self::$params[$key] = self::_filter($value);
            }
            return self::_output();
        }
  
        $queryString = isset($_SERVER['PATH_INFO']) ? self::_pathInfo() : self::_scriptName();
        $queryString = str_replace($config['extension_default'], '', $queryString);
  
        /********配置文件有rewrte规则时,对url进行改写**********/
        if (isset($config['rewrite']) && is_array($config['rewrite'])) $queryString = self::_rewrite($queryString, $config['rewrite']);
  
        /*******获取取controller******/
        if (false === $pos = stripos($queryString, $config['controller_delimiter'])) {
            self::$controller = empty($queryString) ? $config['controller_default'] : self::_filter($queryString);
            self::$action = $config['action_defalut'];
            return self::_output();
        }
        self::$controller = self::_filter(substr($queryString, 0, $pos));
        $queryString = substr($queryString, $pos+1);
  
        /*******获取取action******/
        if (false === $pos = stripos($queryString, $config['action_delimiter'])) {
            self::$action = empty($queryString) ? $config['action_defalut'] : self::_filter($queryString);
            return self::_output();
        }
        self::$action = self::_filter(substr($queryString, 0, $pos));
        $queryString = substr($queryString, $pos+1);
        if (empty($queryString)) return self::_output();
  
        /*******获取取params参数******/
        if (false === $pos = stripos($queryString, $config['params_kv_delimiter'])) {
            return self::_output();
        }
  
        //当params_kv_delimiter与params_delimiter两个分界符相同时
        if ($config['params_kv_delimiter'] == $config['params_delimiter']) {
            $array = explode($config['params_delimiter'], $queryString);
            $count = count($array);
            for ($i = 0; $i < $count; $i++) {
                if ($i%2 == 0) {
                    self::$params[$array[$i]] = isset($array[$i+1]) && !empty($array[$i+1]) ? self::_filter($array[$i+1]) : '';
                }
            }
            return self::_output();
        }
  
        //当params_kv_delimiter与params_delimiter两个分界符不同
        if (false === $pos = stripos($queryString, $config['params_delimiter'])) {
            $array = explode($config['params_kv_delimiter'], $queryString);
            !empty($array[1]) && self::$params[$array[0]] = self::_filter($array[1]);
            return self::_output();
        }
        $array = explode($config['params_delimiter'], $queryString);
        foreach ($array as $value) {
            if (!empty($value)) {
                $arrayTmp = explode($config['params_kv_delimiter'], $value);
                !empty($arrayTmp[1]) && self::$params[$arrayTmp[0]] = self::_filter($arrayTmp[1]);
            }
        }
        return self::_output();
    }
  
    /**
     * php实现的正则rewrite url
     */
    static private function _rewrite($string, $regular) {
        return preg_replace (array_keys($regular), array_values($regular), $string);
    }
  
    /**
     * 对参数进行过滤,防止注入攻击
     */
    static private function _filter($value) {
        return filter::vars($value, '#s_z');
    }
  
    static private function _pathInfo() {
        $queryString = $_SERVER['PATH_INFO'] == '/' ? $_SERVER['PATH_INFO'] = '' : $_SERVER['PATH_INFO'];
        substr($queryString, 0, 1) == '/' && $queryString = substr($queryString, 1);
        return $queryString;
    }
  
    static private function _scriptName() {
        return substr(str_replace($_SERVER['SCRIPT_NAME'], '',$_SERVER['REQUEST_URI']),1);
    
  
    static private function _output() {
        return array(self::$controller, self::$action, self::$params);
    }
}
//$config = array('rewrite' => array('/user\/login\/(.*)/' => "user/login/url_$1"));
//var_dump(router::get());

  评论这张
 
阅读(799)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017