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

后端 未结 9 2054
無奈伤痛
無奈伤痛 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:25

    From Symfony 4.1 inside Service

    class MyService
    {
        private $params;
        public function __construct(ParameterBagInterface $params)
        {
            $this->params = $params;
        }
        public function check($e)
        {
           if($this->params->get("kernel.environment") === "dev") 
           {
                return true;
           }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 09:26

    To get the current environment in a Controller you can use:

    $this->container->getParameter('kernel.environment');
    

    So you just put that in an if() statement to check if it equals to dev.

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

    For Symfony 4.x and above

    If you are using .env to store your environment variables, you can access them in any controller by simply using $_ENV['name-of-variable']

    For a default installation, there is a $_ENV["APP_ENV"] variable available, which will tell you if you are in dev mode or not.


    Use print_r($_ENV); to see all the available environment variables.

    [ps - This also works for Symfony 3.4]


    Reference - https://symfony.com/doc/4.3/components/dotenv.html

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