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
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:
# 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.
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
}
}
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.
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.