我想根据路由参数定义两条路由,例如:
$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/