php - 超薄框架 : Route only accepted parameters

标签 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 对象中添加一些东西来满足我的需求吗?

最佳答案

您可以以匹配某些模式的方式定义路由参数。就您而言,此模式是预定义的单词集:

$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中阅读有关路线模式的更多信息。文档。

关于php - 超薄框架 : Route only accepted parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51032669/

相关文章:

php - 致命的 [0] gearman_packet_unpack_header :invalid command value

ios - 自定义iOS动态框架: Swift class is imported but can't be initialized

php - 使用 PHP 框架会影响性能吗?

php - 无法使用 Slim Framework 读取 "Authorization"HTTP header

php - 使用 phpmailer 在路由中调用函数

php - 如何通过 Yii 2 GridView 中的关系在一列中显示两个属性值

带有包含文件的 php session_start

php - 在 PHP 中使用 count() 计算 PDO 的结果?

javascript - 调用后在 angularjs 中解除 $watch 的绑定(bind)

php - 使用 SLIM3 将 PHP 数组转换为 JSON 数组并获取响应