0

私は、構築中の PHP フレームワークのルーティング システムを構築するアウトライン段階にあります。

きれいな URL には、mod の書き換えを使用する必要があります。その部分をカバーしました。しかし、次のような URL のページを作成したいとします。

www.domain.com/news/10(ニュースID)/

そして、この動的変数 ( This news id ) に書き換え時に名前を付けたい。

私が達成したいのは;

フレームワークはニュース コントローラーにルーティングし、次のように引数として 10 を渡します: $args = array ( 'news_id' => 10 )

4

1 に答える 1

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
    }
}

のマニュアルエントリを参照してください$_SERVER

于 2012-07-09T19:32:25.630 に答える