Slim Framework:仅路由接受的参数

时间:2018-06-25 22:39:40

标签: php frameworks slim

我想基于路由参数定义两条路由,例如:

$app->get('/{param}', function (Request $request, Response $response) {
  // This route can only accept params like: colors, finish, material
}

// And to have another similar but to accept different params

$app->get('/{param2}', function (Request $request, Response $response) {
  // This route can only accept params like: jobs, customers
}

我可以检查它在路由回调中是哪个参数,但是我不认为在这种情况下两个路由回调都被调用,对吗?也就是说,我可以在第一个路由中进行检查,但不会调用第二个路由的回调。

我可以添加一些东西到get对象中来实现我想要的东西吗?

1 个答案:

答案 0 :(得分:3)

您可以以匹配某些模式的方式定义路径参数。在您的情况下,此模式是预定义的一组单词:

$app->get('/{param:colors|finish|materials}', function ( $request, $response, $args) {
  // This route can only accept params like: colors, finish, material
    return "First route with param: " . $args['param'];
});

// And having another route similar but to another params

$app->get('/{param:jobs|customers}', function ($request, $response, $args) {
  // This route can only accept params like: jobs, customers
    return "Second route with param: " . $args['param'];
});

您可以在FastRoute文档中阅读有关路由模式的更多信息。