php访问地址与路由的关系,关于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中找不到匹配的值,能取得默认值

// 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值