doutang1992 2011-01-19 01:51
浏览 54
已采纳

如何从URI中提取页码和ID号?

I am trying to load my whole site through a single point of entry, index.php?uri
My goal is to have nice clean urls though, so far I have come up with...

domain.com/section-name/page-of-that-section

so the first part will load the class/file for the section (mail, users, account, etc.)

The 2nd part will load the class/page for the page of that section

This is pretty straight forward and easy so far. The part that is tricky for me is that some pages will have an ID number (user ID, message ID, blog ID, forum ID, forum post ID, forum topic ID, etc.) this will be used to look up that record in the database by id number.

Next, some pages will also have a page number used for paging.

So sometimes a page number will be present and sometime it will not, same with the id numbers depending on the section it is in.

I am looking for examples, advice, help on getting the page numbers and id numbers part working, I need to be able to get them and assign them to a variable $page_number and $id_number

Below is my code I have so far along with some example url's that I will need to access. I do not want to use an existing framework for this, please help if you can I think I am getting close to getting this to work.

right now I have added id/ in front of id numbers and page/ in front of a paging number, this could be changed to anything (ie; id-423423 page-23 page/23 page23 p/23) as long as I can get the numbers to store to a variable in the PHP

<?php
//get uri
$uri = isset( $_GET['uri'] ) ? $_GET['uri'] : null;

//put uri into array, everything in between '/' will be an array value
$uri1 = explode('/', $uri);

//the first array value will be the SECTION name/class
$section_name = $uri['0'];

//the second array value will always be the PAGE name/class
$page_name = $uri['1'];

//Now some pages will have numbers in the uri
// some are id numbers for things such as a message ID, users ID, comment ID, photo ID, blog ID
// then to complicate it even more sometimes, pages will need paging so will also have a page number in the uri
// I am looking for a way to detect if there is a ID number or page number in uri's for example like listed below.
// right now I have added id/ in front of id numbers and page/ in front of a paging number, this could be changed to anything **(ie; id-423423 page-23 page/23  page23 p/23)** 

mail/inbox/
mail/inbox/page/12
mail/message/id/3432435

users/online/
users/online/page/234
users/profile/id/42234
users/comments/id/42234
users/comments/id/42234/page/3

blogs/list/id/42234
blogs/list/id/42234/page/25
blogs/viewpost/id/1323/             
blogs/create/
blogs/edit/id/34453353

?>
  • 写回答

1条回答 默认 最新

  • douyi8760 2011-01-19 01:57
    关注

    The easiest way is to create a routing system, using regular expressions it's quite an easy task. You can also use some opensource stuff:
    http://robap.github.com/php-router/

    And here is mine for example (simple as a bottle of vodka, but working):

    // for urls like http://mysite.com/1 or http://mysite.com/29 (any digit)
    $ROUTE['(\d*)'] = 'pages/showPage/$1' ;
    // for url http://mysite.com/admin/new-page
    $ROUTE['admin\/new-page'] = 'pages/addPage' ;
    
    function get_route($uri) {
        global $ROUTE ;
        $routes = $ROUTE ;
    
        foreach ($routes as $rUri => $rRoute) {
            if (preg_match("#^{$rUri}$#Ui", $uri)) {
                $route = preg_replace("#^{$rUri}$#Ui", $rRoute, $uri) ;
                break ;
            }
        }
    
        $route = explode('/', $route) ;
        $return['controller'] = array_shift($route) ;
        $return['action'] = array_shift($route) ;
        $return['params'] = empty($route) ? array() : $route ;
    
        return $return ;
    }
    
    //testing for http://mysite.com/2
    $route = get_route( $_GET[ 'uri' ] ) ;
    var_dump( $route ) ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c