Symfony’s Dependancy Injection Container is immensely powerful, allowing one to define a class, or list of classes, that are required to setup a “service” and use it anywhere that is “Container Aware”, that is, anywhere that the Service Container has access to, or has been injected into.
Within your controllers or app classes, something you might need to do pretty often is get access to the Doctrine Entity Manager. This is usually done with the following:
//from with a controller $em = $this->getDoctrine()->getManager(); //or using the container short getter method $em = $this->get('doctrine.orm.entity_manager'); //or from within a container aware context where the container was injected as $container... $em = $container->get('doctrine.orm.entity_manager');
This could get a little annoying if you need to write it over and over again within different methods. Symfony lets us define our own aliases to the predefined default service definitions. In your app-bundle “services.yml”, add the following:
services: em: @doctrine.orm.entity_manager
Now you can access the Doctrine Entity Manager with the following shortcut:
//from within a controller $em = $this->get('em'); //from within a controller aware context which injected the container as $container $em = $container->get('em');
This is just one example, but I hope you can see the power of using aliases to help you access code quicker and become more efficient.
If you have anymore useful short cuts, please feel free to share them in the comments…