Detect if a synthetic service has been set in Symfony2 Dependency Injection container

Posted October 17th, 2014 in Symfony2 by Metod

If you are using the Symfony2 DependencyInjection component, you might have run into synthetic services.

If I quote from the docs:

Synthetic services are services that are injected into the container instead of being created by the container.

So, what do we do if we want to detect if a service has already been injected or not? Turns out that the definition of the synthetic service gets removed when it is injected into the container. So we can just check if the definition for the service exists. Below is a quick gist to get the general idea.

Symfony2: Bundle does not contain any mapped entities

Posted September 14th, 2012 in Symfony2 by Metod

There is one more tip to solving this mistery. Make sure you have a namespace statement in the entity line.


This will cause the error. But if you add the namespace (duh moment) it will work again.


Symfony2: Reload user roles

Posted June 22nd, 2012 in Razno, Symfony2, Tehnikalije by Metod

If you are not using FOSUserBundle for managing your user system, then you might have stumbled onto a problem of reloading user roles when you change them. Here is a possible solution.

Your UserInterface::equals() method might look something like this:

Now all you need to do is change it so it also checks the user roles:

And it should work!

Now whenever you change any user’s roles it will be effective immediately after the next page refresh instead of logout / login.

Symfony2: How to set the correct host for routing in console command

Posted March 31st, 2012 in Symfony2, Tehnikalije by Metod

Quick tip for setting the correct host in your custom symfony2 command.

$this->getContainer()->get('router')->getContext()->setHost('www.example.com');

This will set your host correctly for routing and it will no longer default to ‘localhost’.

A bit longer version: