我在使用 ApiPlatform 的 Symfony 项目中遇到了问题。每当我在 URL 中包含自定义参数时,我都会遇到 NotFoundHttpException,并显示消息“无效的 URI 变量”。看来 Symfony 中的 ApiPlatform 正在将我的自定义 URL 参数解释为标识符而不是变量。
以下是相关代码片段:
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/{tenant}/agreement',
openapi: new Model\Operation(
parameters: [
new Model\Parameter(
name: 'tenant',
in: 'path',
required: true,
),
],
),
),
]
)]
我的目标是在 SQL 查询条件中包含此自定义参数以检索集合。
我怀疑ApiPlatform可能误解了参数配置。我正在寻求有关如何解决此问题并确保 ApiPlatform 将自定义 URL 参数作为变量而不是标识符正确处理的指导。任何有关解决此问题的建议或见解将不胜感激。谢谢!
我找到了解决方案:
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/{tenant}/agreement',
uriVariables: [],
routeName: 'api_agreement',
openapi: new Model\Operation(
parameters: [
new Model\Parameter(
name: 'tenant',
in: 'path',
required: true,
),
],
),
),
]
)]
必须设置 uriTemplate 和 RouteName 才能正确处理路由。此外,设置 uriVariables: [] 允许“禁用”“id”参数。
然后,您可以创建一个控制器,为其分配一条路由(与实体中指示的路由相同),这允许手动处理请求(我们需要自己生成响应)。
我找不到更好的解决方案,但如果这个可以帮助某人,那么值得分享。如果您有更好的解决方案,欢迎分享。