1

I would like to access Symfony 3's Kernel.getEnvironment() in a service implementing the ContainerAwareTrait.

I am wondering whether I should implement an instance of the Kernel object or whether one is already created by the application itself. If so, how can I retrieve the instance?

My application is a clone of the Symfony 3 demo application.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 1
    Might be worth reading through the [Symfony Recommended Practices](https://symfony.com/doc/current/best_practices/index.html) document. – Cerad Dec 13 '17 at 12:50

1 Answers1

2

Couldn't you just inject %kernel.environment% into your service?

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • 1
    Maybe, but I see in another question that $this->getContainer()->getParameter('kernel.environment'); might work too. What do you think? – Jérôme Verstrynge Dec 13 '17 at 09:22
  • 3
    It will, but keeping your dependencies explicit (through the service defintion) is much cleaner – Nico Haase Dec 13 '17 at 09:23
  • 2
    also, avoiding to use the complete container is kinda a best pratice, especially when all you need is a small information easily injected, – NaeiKinDus Dec 13 '17 at 09:33