2013-10-24 39 views
0

我有路由查詢是一個MySQL數據庫爲打印作業REST路線

http://api.com/prints/

我想根據日期,以便於查詢結果...

http://api.com/prints/YYYY

http://api.com/prints/YYYY/MM

http://api.com/prints/YYYY/MM/DD

但使用相同的http://api.com/prints/路線。每張照片都有一個TIMESTAMP

有沒有人有一個很好的例子來說明如何做到這一點?

回答

0

在可選的參數,可以你需要使用括號:

$app->get('/prints(/:year)(/:month)(/:day)', function ($year = '', $month = '', $day = '') { 
    printf('%s-%s-%s', $year, $month, $day); 
}); 
0

我會使用的查詢參數實現這一點,即:

http://api.com/prints?timestamp=YYYY-MM-DD

+0

你將如何處理這個月和一天的失蹤? – maxum

+0

如果您需要支持:'http://api.com/prints?year = YYYY','http://api.com/prints?year = YYYY&month = MM'和'http:// api。 COM /打印?年= YYYY&月= MM&天= DD'。 –

+0

好吧,現在我看到你用PHP,mysql和slim標記了這個標籤。國際海事組織,這是不夠的。你需要解釋你正在尋找你的問題和/或主題的PHP解決方案。我以爲你在問一個普通的REST方法問題,因爲你用'rest'標記了這個問題。 –

0

基於當前的例子official Slim documentation

$app->get('/prints(/:year(/:month(/:day)))', function ($year = null, $month = null, $day = null) { 
    if ($day !== null) { 
     // return daily overview resource 
    } else if ($month !== null) { 
     // return monthly overview resource 
    } else { 
     // return yearly overview resource 
    } 

    // return some error or (better) overview resource on available data/endpoints 
});