New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect ServiceCircularReferenceException? #2347

Closed
lmcd opened this Issue Oct 7, 2011 · 6 comments

Comments

Projects
None yet
5 participants
@lmcd
Contributor

lmcd commented Oct 7, 2011

I've written a service that gets invoked inside a Twig template, which has a dependancy on the 'Templating' service. This results in a ServiceCircularReferenceException. Commenting out the exception causes the code to run fine.

@stof

This comment has been minimized.

Member

stof commented Oct 7, 2011

Circular references in the services can lead to incorrect creation of the objects, eventually leading to infinite loops. The issue is that each service requires the other one to be created before itself being created.
The correct way is to avoid circular references

@lmcd

This comment has been minimized.

Contributor

lmcd commented Oct 7, 2011

So my service must somehow be passed TwigEngine from within my twig template?

@fabpot

This comment has been minimized.

Member

fabpot commented Nov 22, 2011

The templating service relies on a lot of other services, so it is rather easy to have this circular dependency problem. You The exception should tell you for which service there is a problem. The solution depends on the service for which there is a circular reference problem, but one simple solution is to inject the container and get the service when you have a need for it.

@bmeynell

This comment has been minimized.

bmeynell commented Apr 17, 2014

It's been 3 years. It this possible yet (to not have to inject the container into a twig extension when you only need the templating service)?

@bmeynell

This comment has been minimized.

bmeynell commented Apr 17, 2014

It's been 3 years. It this possible yet (to not have to inject the container into a twig extension when you only need the templating service)?

Answering my own question here. I think I can use Twig_Environment::display() to accomplish rendering of a template from a twig extension.

@apfelbox

This comment has been minimized.

Contributor

apfelbox commented Apr 23, 2014

@bmeynell I think what @fabpot was trying to say is that it is practically impossible: you need the twig service for the extension, but you need the extension for the twig service.

So you have the circular reference, as thrown.
load twig -> load extension -> load twig -> load extension -> ...

It would only be possible if the twig service is only loaded when you actually need it (instead of on creation time) - which you do by inserting the whole service container and requesting the service in your method.

tijsverkoyen added a commit to sumocoders/FrameworkErrorBundle that referenced this issue Feb 25, 2015

Integrates Errbit (JS and PHP errors)
It seems like it is not possible to only inject the templating See
symfony/symfony#2347 (comment) for
more information.

j0k3r added a commit to j0k3r/two-factor-bundle that referenced this issue Dec 22, 2015

Avoid using templating service which might throw ServiceCircularRefer…
…enceException

It seems that when using templating service we can ran into a ServiceCircularReferenceException because it "relies on a lot of other services, so it is rather easy to have this circular dependency problem".

The "best", and easiest, solution (which isn't a best practice btw) it to inject the container and then get the templating service.

symfony/symfony#2347

j0k3r added a commit to j0k3r/two-factor-bundle that referenced this issue Dec 22, 2015

Avoid injecting templating service in constructor
It seems that when using templating service we can ran into a ServiceCircularReferenceException because it "relies on a lot of other services, so it is rather easy to have this circular dependency problem".

The "best", and easiest, solution (which isn't a best practice btw) it to inject the container and then get the templating service.

See: symfony/symfony#2347

But if we don't inject in the constructor but use assigment instead, we don't have the problem.

@j0k3r j0k3r referenced this issue Dec 22, 2015

Closed

Symfony 3.0 #34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment