Slim Framework:仅路由接受的参数
Slim Framework: Route only accepted parameters
我想根据路由参数定义两条路由,例如:
$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 文档中阅读更多关于路由模式的信息。
我想根据路由参数定义两条路由,例如:
$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 文档中阅读更多关于路由模式的信息。