2

我想像这样使用 Slim Framework v3.2.0 的子路由:

  • www.test.com/ <-- 索引页
  • www.test.com/foodtype/ <--单独的页面
  • www.test.com/foodtype/page/ <--foodtype 的子类别

据我了解,只能调用一次 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'] 
});

如何为 page1 添加单独的可选路由?

我试过了:

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

这会导致“找不到页面”错误。我想我也需要转义可选的“/”?

4

1 回答 1

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']
});
于 2016-03-03T11:56:16.213 回答