Check if in “dev” mode inside a Controller with Symfony

后端 未结 9 2053
無奈伤痛
無奈伤痛 2021-01-01 08:56

When using dev mode with a Symfony2.x application, one usually works in locale. Hence, such function does not works as expected (for instance, try to get th

相关标签:
9条回答
  • 2021-01-01 09:07

    As of Symfony 2.5 it could be done as:

    $this->container->get('kernel')->getEnvironment();
    

    Directly asking Kernel of it's environment looks nicer than searching for parameter.

    0 讨论(0)
  • 2021-01-01 09:10

    For Symfony 4.1+

    I prefer to avoid using kernel inside my controllers and services.

    services.yaml:

    parameters:
        app.environment: '%kernel.environment%'
        ...
    

    Your controller / service:

    class MyService
    {
        private $isDev;
    
        public function __construct(ParameterBagInterface $appParams)
        {
            $this->isDev = $appParams->get('app.environment') === 'dev';
        }
    
        public function doSomething()
        {
            if ($this->isDev)
            {
                ...
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 09:12

    Symfony 3.3/4/5+ Solution

    Here is 2017 and Symfony 3.3+ version using Constructor Injection.

    Instead of passing you whole application (= container), you could pass only the parameter you need:

    1. Service config

    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        App\Controller\SomeController:
            arguments: ['%kernel.environment%']
    

    If you don't understand this syntax, check this post explaining Symfony DI news in before/after examples.

    2. The Controller

    namespace App\Controller;
    
    final class SomeController
    {
        /**
         * @var string
         */
        private $environment;
    
        public function __construct(string $environment)
        {
            $this->environment = $environment;
        }
    
        public function someAction()
        {
            $this->environment...
            // any operations you need
        }
    }
    


    Why to avoid passing Container in Controller?

    The most important thing in the code is consistency.

    • If you prefer static and service locators (= service you can pass anywhere to get any other service), use it.

    • If you prefer constructor injection, tree dependency graph (!= circular dependencies), use it.

    Mixing this concept might be ok for you, if you know why you used them that way. But here comes to play The Broken Window Theory (nicely described version by Coding Horror). Anyone coming to the code will more likely pick the version that is not intended to use that way.

    Ambiguity in the code is the first invitation to legacy code

    I've mentored teams of many applications, that started with simple $this->container in the code and after couple of years ended up calling me for help, how to rewrite or refactor whole static hell.

    0 讨论(0)
  • 2021-01-01 09:14

    In Symfony 4 and higher, you can use simply :

    if ($this->getParameter('kernel.environment') === 'dev') {
        // ...
    }
    
    0 讨论(0)
  • 2021-01-01 09:20

    Since you want to know if you are in dev mode (not necessarilly the environment named "dev"), you can retrieve the kernel from the service container and check the isDebug method return:

    $kernel = $this->get('kernel');
    $devMode = $kernel->isDebug();
    

    As noted in the documentation (emphasis is mine),

    Important, but unrelated to the topic of environments is the true or false argument as the second argument to the AppKernel constructor. This specifies if the application should run in "debug mode". Regardless of the environment, a Symfony application can be run with debug mode set to true or false. This affects many things in the application, such as displaying stacktraces on error pages or if cache files are dynamically rebuilt on each request. Though not a requirement, debug mode is generally set to true for the dev and test environments and false for the prod environment.

    Internally, the value of the debug mode becomes the kernel.debug parameter used inside the service container.

    0 讨论(0)
  • 2021-01-01 09:21

    Symfony 5.1

    $this->devEnvironment = 'dev' === getenv('APP_ENV');

    I use this inside of a Symfony service. You will need to add the line APP_ENV=dev to .env.local and APP_ENV=prod to .env file. Depending on if you have a similar env setup to me

    0 讨论(0)
提交回复
热议问题