PHP Slim Framework 路由多资源请求和单资源请求

PHP Slim Framework routing multi and single resource requests

我正在尝试在我的 Slim 应用程序中创建 2 条路由 来处理 单一资源 GET 请求。

例如:

/surveys 将 return 所有调查

/surveys/3 将 return id 为 3

的调查

但是以下会产生服务器错误:

$app->get('/surveys', function ($request, $response, $args) {

     // Code here

});

$app->get('/surveys/{id}', function ($request, $response, $args) {

     // Code here

});

我该怎么做?

谢谢

我会这样写:

$app->group('/surveys', function () use($app) {
     $app->get('', function () {

          // Endpoint for '/surveys'

     });

     $app->get('/{id}', function ($id) {

          // Endpoint for '/surveys/{id}'

     });
});

问题是我有第三条路线:

$app->get('/surveys/count', function ($request, $response, $args) {

    // code here

});

我没有在意,但它弄乱了 {id} 行进 "count" 词的第二条路线。由于我没有启用错误处理程序(感谢 Davide Pastore),我找不到问题所在。

我把第二条路线改成了:

$app->get('/surveys/{id:[0-9]+}', function ($request, $response, $args) {

    // code here

});

现在一切正常!

感谢您的帮助!