2011-01-19 34 views
0

我想通過一個入口點加載我的整個網站,的index.php?URI
我的目標是有很好的清潔網址雖然,到目前爲止,我還拿出...如何從URI中提取頁碼和ID號碼?

域.COM /部分名稱/頁的 - 即截面

所以第一部分會加載爲部分類/文件(郵件,用戶賬號等)

第2部分將加載頁面的類/頁面該部分

這是非常直截了當,容易到目前爲止。對我來說很棘手的部分是,一些頁面將有一個ID號碼(用戶ID,消息ID,博客ID,論壇ID,論壇帖子ID,論壇主題ID等),這將用於查找通過ID號在數據庫中記錄。

接下來,一些頁面也會有一個頁碼用於尋呼。

所以有時頁碼將出席並有時不會,同樣具有取決於它在部分ID號。

我找實例,諮詢,得到的頁碼幫助和ID號的一部分工作,我需要能夠讓他們並將其分配給一個變量$ PAGE_NUMBER$ ID_NUMBER

下面是我的代碼,我到目前爲止以及一些示例URL的,我需要訪問。我不想爲此使用現有的框架,請幫助,如果你能我認爲我正在接近這個工作。

現在我在id號碼和頁面前面加了一個分頁號碼,這可以改成任何東西(ie; id-423423 page-23 page/23 page23 ​​p/23)只要我能得到的數字存儲到一個變量在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 

?> 

回答

2

最簡單的方法是創建一個路由系統,使用正則表達式這是一個很容易的事。你也可以使用一些開源的東西:
http://robap.github.com/php-router/

這裏是我的例子(簡單一瓶伏特加,但工作):

// 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) ; 
+0

感謝這看起來像一個很好的解決方案。看完我的代碼後,對於我來說,使用像index.php這樣的部分可能會是更好的選擇。section = mail&page = read&pagenumber = 12,然後使用.htaccess文件將其更改爲domain.com/mail/read/page-12 – JasonDavis

+0

那麼,你的選擇。但是,使用單個get-param並解析它是一種常見做法,而不是爲所有類型的http查詢存儲一堆重寫規則。爲此,MVC和普通查詢(即index.php?uri = param1/param2 /.../paramN,沒有任何get-key,除了'uri')服務良好 – kos

+0

這看起來很完美,謝謝 – JasonDavis