日韩精品中文字幕一区二区-日韩精品中文字幕一区-日韩精品中文字幕视频-日韩精品在线一区二区三区-第一页在线-第一福利视频

當前位置: 首頁編程開發PHP → 關于php url路由的實現

關于php url路由的實現

更多

這篇文章提供分享給大家,是關于php url路由的實現,下面的詳細的解析,希望對各位有所幫助。

1.符合規則定義的偽靜態訪問路徑解析

對于"test.php/user/lists/normal/id/2.html" 可解析為
control = user,action = lists,filter = normal,order = id,curPage = 3

對于"test.php/users/lists.html" 可解析為
control = user,action = lists,filter = all,order = '',curPage = 1 可取得規則定義中的默認值

2.不符合規則定義的偽靜態路徑解析

action,control 不符合規則
對于"test.php/users/lists/all/id1/1.html" 報錯
試圖訪問不存在的頁面

不符合匹配模式
對于"test.php/user/lists/all/id1/1.html" 可解析為
control = user,action = lists,filter = all,order = '',curPage = 1
可取得不符合匹配模式項目的默認值,上例 order 不符合匹配模式

定義路由規則時可以定義默認值,當在pathinfo中找不到匹配的值,能取得默認值

<?php

// url 路由規則定義

$urlRule = array(

    'user' => array(            // control

        'lists' => array(       // action

            //'名稱'    => '默認值,值模式匹配'

            'filter'    => 'all,^(all|normal|admin)$',

            'order'     => ',^-?[a-zA-Z_]+$',

            'curPage'   => '1,^[0-9]+$',

          ),

    ),

);

function parseUrl(){

        $queryString = array();

        $GLOBALS['control'] = 'index';

        $GLOBALS['action'] = 'index';

        if (isset($_SERVER['PATH_INFO'])){

                //獲取  pathinfo

                $aPathInfo = explode('/', substr($_SERVER['PATH_INFO'], 1, strrpos($_SERVER['PATH_INFO'], '.')-1));

                // 獲取 control

                $GLOBALS['control'] = $aPathInfo[0];

                array_shift($aPathInfo);

                // 獲取 action

                $GLOBALS['action'] = (isset($aPathInfo[0]) ? $aPathInfo[0] : 'index');

                array_shift($aPathInfo);

                // 獲取 入口文件名

                $GLOBALS['PHP_SELF'] = str_replace($_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF']);

                $queryString = $aPathInfo;

        }

        parseQueryString($queryString);

}

function parseQueryString(array$aQueryString){

        $queryString = array();

        // control 與 action 為默認值時 

        if ($GLOBALS['control'] == 'index' && $GLOBALS['action'] == 'index'){

                $GLOBALS['queryString'] = $queryString;

                return true;

        }

        global $urlRule;

        if (isset($urlRule[$GLOBALS['control']][$GLOBALS['action']])){

                $aActionRule = &$urlRule[$GLOBALS['control']][$GLOBALS['action']];

                foreach ($aActionRule as $key=>$val){

                        // 規則值為 '' 時

                        if ($val == '') {

                                $queryString[$key] = '';

                                continue;

                        }

                        if (isset($aQueryString[0])){

                                // 取得正則表達式

                                $pattern = '/'.substr($val, strpos($val, ',')+1).'/';

                                // 模式匹配

                                if (preg_match($pattern, $aQueryString[0])){

                                        // 取值

                                        $queryString[$key] = $aQueryString[0];

                                        // 彈出值

                                        array_shift($aQueryString);

                                }else {

                                        // 取默認值

                                        $queryString[$key] = substr($val, 0, strpos($val, ','));

                                }

                        }else {

                                // 取默認值

                                $queryString[$key] = substr($val, 0, strpos($val, ','));

                        }

                }

                $GLOBALS['queryString'] = $queryString;

        }else {

                throw new Exception('試圖訪問不存在的頁面');

        }

}

parseUrl();

var_dump($GLOBALS['control']);

var_dump($GLOBALS['action']);

var_dump($GLOBALS['queryString']);

?>

**
         * Pathinfo函數
         * 功能:將URL中的Pathinfo解析為$_GET全局變量
         * 返回值:解析成功返回true否則為fasle
         * 例如:http://hostname/page.php/argv/argc/a/1/b/2.html
         * 將會解析為$_GET['argv']='argc';$_GET['a']=1;$_GET['b']=3;
         */
        function pathinfo(){
                $pathinfo=explode('/',$_SERVER['PATH_INFO']);
                $count=count($pathinfo);
                for($foo=1;$foo<$count;$foo+=2){
                        $_GET[$pathinfo[$foo]]=($foo+2)==$count?array_shift(explode('.',$pathinfo[$foo+1])):$pathinfo[$foo+1];
                }
        }
        /**
         * rewrite函數
         * 功能:
         */
        Function rewrite($url=null){
                return REWRITE?$url.'.html':$_SERVER['PHP_SELF'].$url.'.html';
        }

熱門評論
最新評論
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數: 0/500 (您的評論需要經過審核才能顯示)
主站蜘蛛池模板: 孙兴电影| 意外的春天 电影| i性感美女视频| 学校要的建档立卡证明| 邵雨薇为艺术贡献的电影有哪些| 香谱七十二法图大全| 帮妈妈做饭看图写话二年级| 火辣身材| 急招55岁左右男司机| 49图库龙门客栈| 布袋头| 部队换季保养广播稿| 自相矛盾视频故事视频| 原神黄色漫画| 双生姐妹| 咖啡爱上香草电视剧| 玉林电视台| 我们的日子电视剧演员表| 美国电影waseas| 璐·德·拉格| 高登眼镜| 浙江卫视今天节目单| 姐夫操小姨子| 打开免费观看网站| 我的幸福婚约| 成龙版杨家将电影免费播放| 昌秀 电影| 五上竖式计算300道及答案| 吴京电影大全| soldier's heart| 欧美日韩欧美日韩在线观看视频| 徐若晗全部电视剧大全| 西藏卫视节目单| 暴风雨的儿女| 妇女停经前有什么征兆| 芭芭拉·布薛特| 《棋魂》电视剧| 五年级上册白鹭笔记| 王音棋的个人简历| 罗斯福游戏| 赵立军|