You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

API Platform配置问题:如何仅在非生产环境启用Swagger UI

解决生产环境无法禁用Swagger UI的问题

你想要仅在非生产环境开放Swagger UI,按教程配置后未生效,当前api_platform.yaml配置如下:

api_platform:
  defaults:
    route_prefix: '/%env(API_ROUTE)%'
    enable_docs: false
    enable_entrypoint: false
    enable_swagger_ui: false
  swagger:
    api_keys:
      JWT:
        name: Authorization
        type: header
  collection:
    pagination:
      enabled: true

有效解决方案

1. 按环境拆分配置

直接在主配置中全局关闭Swagger相关开关无法实现"仅非生产环境开启"的需求,需通过环境专属配置文件拆分控制:

  • 主配置文件(config/packages/api_platform.yaml)保留基础配置,移除全局docs相关设置:
api_platform:
  defaults:
    route_prefix: '/%env(API_ROUTE)%'
  swagger:
    api_keys:
      JWT:
        name: Authorization
        type: header
  collection:
    pagination:
      enabled: true
  • 在dev环境配置文件(config/packages/dev/api_platform.yaml)中单独开启Swagger功能:
api_platform:
  defaults:
    enable_docs: true
    enable_entrypoint: true
    enable_swagger_ui: true

2. 清理生产环境缓存

配置修改后必须清除生产环境缓存,确保新配置生效:

php bin/console cache:clear --env=prod

3. 排查配置覆盖情况

检查是否存在config/packages/prod/api_platform.yaml这类生产环境专属配置文件,或第三方扩展强制开启了Swagger UI。

内容的提问来源于stack exchange,提问作者monaxi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:07:13