2012-07-09 64 views
0

我正在構建一個構建我正在構建的PHP框架的路由系統的概述階段。漂亮的URL和動態變化

我將需要使用國防部重寫,爲漂亮的網址。我覆蓋了那部分。 但說我想要做一個頁面一樣的網址:

www.domain.com/news/10(News-id)/

,我想這個動態的變量(此消息ID)有重寫時的名字。

我想達到的是;

框架路由消息的控制器,並通過10作爲參數爲: 的$ args =陣列( 'news_id'=> 10)

回答

1

可以使用$_SERVER超級全局檢查所請求的URI。在你的榜樣,$_SERVER['REQUEST_URI']將被設置爲一樣的東西:

/news/10/ 

然後,您可以從該字符串得到所要求的消息-ID。

更新

// Use substr to ignore first forward slash 
$request = explode('/', substr($_SERVER['REQUEST_URI'], 1)); 
$count = count($request); 

// At this point, $request[0] should == 'news' 
if($count > 1 && intval($request[1])) { 
    // The second part of the request is an integer that is not 0 
} else { 
    if($count == 1) { 
     // This is a request for '/news' 

    // The second part of the request is either not an integer or is 0 
    } else if($request[1] == 'latest') { 
     // Show latest news 
    } else if($request[1] == 'oldest') { 
     // Show oldest news 
    } else if($request[1] == 'most-read') { 
     // Show most read news 
    } 
} 

manual entry$_SERVER

+0

但棘手部分時URL並不總是一個新聞-ID。 - 它可能是:新聞/最新/ – Kao 2012-07-09 19:33:17

+0

一點也不棘手,請參閱更新 – 2012-07-09 19:39:59

+0

好的,所以我必須從PHP內部連接它。希望得到一些很酷的mod_rewrite的東西,但是meh ... :)謝謝。 – Kao 2012-07-10 11:13:54