Vael Victus
Vael Victus

Reputation: 4122

Dynamically disable the web toolbar and profiler in Symfony 4 using .env

The web profiler is set to activate if APP_ENV is dev. It is this way on our staging server; however, a security audit is being run on our staging server and it is required that we manually turn the profiler off while keeping APP_ENV = dev.

This will successfully disable the profiler and toolbar:

web_profiler:
    toolbar: false
    intercept_redirects: false

framework:
    profiler: 
      { enabled: false, only_exceptions: false }

But I want to use .env to use flags that we can control to disable each. When I try to, Symfony complains:

Environment variables "bool:SYMFONY_TOOLBAR" are never used. Please, check your container's configuration.

This leads me to an answer here, which claims:

profiler > enabled cannot be set with a runtime env variable, because that controls whether all the profiler services are created in the container (wrapping services whenever needed to be able to profile them). Changing the container entirely cannot be done at runtime (and the value of this boolean config does not end up being set itself anywhere in the container, which is why this error is triggered)

The developer says "Use a parameter in a file that is only loaded in dev mode." but I have no idea what that means; so, how can I resolve this? (.env is not a requirement, just ideal)

Upvotes: 3

Views: 1892

Answers (1)

Vael Victus
Vael Victus

Reputation: 4122

It is impossible to use environment flags in web_profiler.yaml due to the order in which the framework loads its environment variables. Thankfully it isn't too hard or scary to just make a new environment:

https://symfony.com/doc/4.1/configuration/environments.html#creating-a-new-environment

Create your environment, save your new web_profiler.yaml under it, then add it to bundles.php's relevant lines; in my case, my line reads like this after adding the staging environment:

Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true, 'staging' => true],

Upvotes: 1

Related Questions