2015-04-23 60 views
1

我想定義一個最後有一個.suffix參數的路由。我想在我的應用程序中使用它來返回用戶需求。例如.json或.xml或者沒有!我應該如何定義我的路線?這是一個例子地址欲:苗條的可選路由參數後綴不起作用

/user/all.json 

/user/all.xml 

/user/all # default json 

這是我所定義的路由。但它沒有按預期工作。

/user/:method(\.(:type)) 
+1

有一個接受這個頭:HTTP://www.w3 .org/Protocols/rfc2616/rfc2616-sec14.html – Tuim

+0

在這個項目中,我需要讓用戶確定輸出格式 – Reza1607

回答

1

Slim路由段由正斜槓定義,不能與點互換。但是,您可以將邏輯添加到路由關閉中,或將路由條件添加到多個路由。

在封閉:

$app->get('/user/:methodtype', function ($methodtype) { 
    if ($path = explode($methodtype, '.')) { 
     if ($path[1] === 'json') { 
      // Something to do with json... 
     } elseif ($path[1] === 'xml') { 
      // Something to do with xml... 
     } 
    } 
}); 

或者使用路徑的條件下,爲每個所以他們是互相排斥的:

// JSON requests 
$app->get('/user/:methodtype', function ($methodtype) { 
    // Something to do with json... 
})->conditions(array('methodtype' => '.json$')); 

// XML requests 
$app->get('/user/:methodtype', function ($methodtype) { 
    // Something to do with xml... 
})->conditions(array('methodtype' => '.xml$'));