Skip to content
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
Closed

Incorrect ServiceCircularReferenceException? #2347

lmcd opened this issue Oct 7, 2011 · 6 comments

Comments

@lmcd
Copy link
Contributor

@lmcd 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
Copy link
Member

@stof 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
Copy link
Contributor Author

@lmcd lmcd commented Oct 7, 2011

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

@fabpot
Copy link
Member

@fabpot 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
Copy link

@bmeynell 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
Copy link

@bmeynell 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
Copy link
Contributor

@apfelbox 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
It seems like it is not possible to only inject the templating See
symfony/symfony#2347 (comment) for
more information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants