2016-03-02 52 views
2

我想用子路線搭配修身框架V3.2.0像這樣:修身框架子路由

  • www.test.com/ < - 索引頁
  • www.test.com/食物類型/ < - 單獨的頁面
  • www.test.com/foodtype/page/ < - 食物類型子類別

據我所知,只有一個get可以被調用。目前我有這在我的routes.php文件:

$app->get('/', function() { 
// Load index page 
}); 

$app->get('/{foodtype}', function ($request, $response, $args) { 
// Load page based on the value of $args['foodtype'] 
}); 

如何添加單獨的可選路線第1頁?

我已經試過:

$app->get('/{foodtype}/{page}', function ($request, $response, $args) { 
// Load page based on the value of $args['foodtype'] and $args['page'] 
}); 

這將導致 '找不到網頁' 的錯誤。我認爲我需要逃避可選的'/'?

回答

2

您必須在原始路線中將頁面部分設爲可選。

如:

$app->get('/{foodtype}', function ($request, $response, $args) { 
// Load page based on the value of $args['foodtype'] 
}); 

變爲:

$app->get('/{foodtype}[/{page}]', function ($request, $response, $args) { 
// Load page based on the value of $args['foodtype'] and $args['page'] 
});