-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Templating] Circular reference detected for service "security.context" #11087
Comments
the issue is that twig depends on the security context (one of the extension does actually), which itself depend on your grant_type service, which itself depend on your |
why does the UserService depend on the NotificationService here ? |
Because on UserService::registerUser() I want to use the NotificationService to send a mail (NotificationService::sendRegistrationMailToUser()). NotificationService is just a facade for MailService and PushService (it allows me to inject only one service instead of to, if someday I want to send push notifications) If I just link to MailService, the circular dependence will still be there, btw. |
My suggestion would be to have UserService dispatch an event when registering a user. Then, you can plug the listeners you want on this event (for instance a NotificationListener). Given that the event dispatcher loads listener lazily when dispatching, this would break the circular reference here (and it would also give you more flexibility if you want to perform another action on registration later, as you could simply register another listener without having to modify the UserService) |
It sounds good. It makes sense to me that notifications are less related to business logic than the registerUser(setters, validation, persist). Does this solution affect the performances in a bad way? |
the overhead of dispatching the event rather than calling each method directly is probably negligeable compared to the business logic making DB calls. and for other places using the other methods of the UserService, it might even avoid instantiating the MailService entirely (assuming you don't have another place requiring to instantiate it of course). |
At the moment, I'm just starting developping the NotificationService. But, indeed, it will be used in other bundles, in other services. If using the EventDispatcher is a good way to make a "clean&clear" solution, I can totally start with that. Plus, you're right, the NotificationService is only used once in the UserService (send mail on register) even if this service only expose 3 functions, but I think some bigger services will use it for like one call in a total of 10 functions (and not the most called functions). So it can be good to avoid instanciate the NotificationService everytime. |
Can we close this issue ? |
Of course! Thank you a lot for your help :) |
Just to confirm, it works really well. I made my NotificationService an EventSubscriber and I made it subscribe to the user_registration event. And instead of having a dependecy with NotificationService in UserService, I use EventDispatcher to be able to dispatch the user_registration event. Obviously, I can now use the templating service inside my mailer without having any circular reference. |
I tried to inject the templating service and this is what I get.
If I comment, or remove, the templating service from the dependency, everything is working well. I saw old issues about that, but it seems I'm the only one at the moment experiencing this. Am I doing something wrong?
Composer.json
Composer.lock
Thanks,
Litz.
The text was updated successfully, but these errors were encountered: