One solution is to build your container with containerbuilder. But that only builds your module container. If you have dependencies to other libs it's hard to resolve all the dependencies. Another drawback is, that you build your container multiple times.
On simple solution is to use your AppKernel directly to get the container. AppKernel extends abstract Kernel. So you can override getContainer and save the container in a static field.
Add this 2 methods and the static field inside your AppKernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @var \Symfony\Component\DependencyInjection\Container | |
*/ | |
private static $_container; | |
/** | |
* override parent getContainer() and save container staticly | |
*/ | |
public function getContainer() | |
{ | |
if(!self::$_container) | |
{ | |
self::$_container = $this->container; | |
} | |
return $this->container; | |
} | |
/** | |
* @return \Symfony\Component\DependencyInjection\Container | |
*/ | |
public static function getContainerStatic() | |
{ | |
return self::$_container; | |
} |
AppKernel::getContainerStatic()
Keine Kommentare:
Kommentar veröffentlichen