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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么