How to disable Nelmio UI in production?
@gp_sflover's comment got me on the right track, but there's more to it than just disabling NelmioApiDocBundle on prod in AppKernel.php
. Configs & routes that refer to Nelmio will generate errors until you move them into dev-specific files. The following change in app/AppKernel.php
was the first step:
public function registerBundles(){ $bundles = [ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), ... // new Nelmio\ApiDocBundle\NelmioApiDocBundle(), // <-- REMOVED FROM HERE new Nelmio\CorsBundle\NelmioCorsBundle(), new AppBundle\AppBundle(), ]; if (in_array($this->getEnvironment(), ['dev', 'test'], true)) { $bundles[] = new Nelmio\ApiDocBundle\NelmioApiDocBundle(); // <-- ADDED HERE $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle(); ...
To eliminate the config errors, I had to move the following stuff out of app/config/config.yml
and into config_dev.yml
:
# nelmio Configurationnelmio_api_doc: sandbox: enabled: true name: 'DLAP API Bridge' swagger: ... cache: enabled: false
Likewise, the following stuff came out of app/config/routing.yml
and moved to routing_dev.yml
:
NelmioApiDocBundle: resource: "@NelmioApiDocBundle/Resources/config/routing.yml" prefix: /api/docnelmio_api_swagger: resource: "@NelmioApiDocBundle/Resources/config/swagger_routing.yml" resource: null prefix: /api-docs
Now with symfony4 and flex you can install the bundle normally
composer require nelmio/api-doc-bundle
(So it won't be set as a require-dev
depandency in your composer.json
)
Now you change the setting in the project/config/bundles.php
to:
Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['dev' => true]
So you don't get errors (eg from annotations), but it wont be loaded from Kernel.php
not loading the bundle in config/bundles.php throws an exception in Symfony 4.3 with NelmioApiDocBundle Version 3:
There is no extension able to load the configuration for "nelmio_api_doc"
I ended up disabling the route for the api docs with a redirect in the environment, which needed to be disabled (prod):
#config/routes/prod/nelmio_api_doc.yamlapp.swagger: path: /api/doc.json methods: GET defaults: _controller: FrameworkBundle:Redirect:urlRedirect path: / permanent: trueapp.swagger_ui: path: /api/doc methods: GET defaults: _controller: FrameworkBundle:Redirect:urlRedirect path: / permanent: true