Dieses Blog durchsuchen

Samstag, 31. Dezember 2016

Symfony get container from everywhere

In some situations your context is not container aware. For instance you are somewhere in your application and you want to dispatch an event. Without container you won't get the configured dispatcher.

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

After that you can get the loaded and configured container like that:

AppKernel::getContainerStatic()

Keine Kommentare:

Kommentar veröffentlichen