-1

I'm migrating a legacy PHP project (pre-OO) to Symfony2. On every request I have to:

  • compute some dynamic data (depending on the current date and/or some request parameter)
  • use that data (multiple times!) in the rendered response.

A naive approach would be:

  • At the start of every controller method, call some global helper function to compute the data.
  • At the end of every controller method, pass the data as a parameter to the twig template.

Sounds tedious. Maybe it would be better to:

  • Create a subscriber for request events that computes the data when a request comes in and provides access to it via getter methods.
  • Define that subscriber/service as a global twig variable in config.yml.
  • In twig templates, call the getter methods on that service as needed.

Is that viable? In particular, are the twig variable/service and the subscriber always identical? Or could the service be a newly created instance?

Is this some sort of misuse? Or is there an officially recommended way for such a use case?

EDIT The data is not only needed in every twig template but in some controllers, too.

3
  • If you only need this information in twig templates then just create a twig extension and add your getters there. That eliminates the need for a subscriber. I would suggest taking the time to see if you really need this sort of data. I know you are dealing with a legacy app but there may be better designs. Commented Nov 1, 2016 at 13:07
  • Unfortunately, this information is also needed in some controllers. Edited my question accordingly. Commented Nov 1, 2016 at 13:27
  • In which case, a Subscriber is your starting point. Not a big fan of having a bunch of global data so be careful. Globals can make your code difficult to maintain. Commented Nov 1, 2016 at 13:44

1 Answer 1

0

Calling a specific method in every Controller-Action would really be a bad solution. Your solution to use a subscriber isn't perfect either.

Without knowing your use-case in detail it´s hard to find a suitable way. Maybe one approach would be to write a Twig-Extension and injecting a Service into this Extension. The Service would get the Request-Stack via Dependency-Injection and compute the relevant Data. You could then access this data "on demand" through the Twig-Extension during the rendering.
Another approach could be using sub-requests during the rendering (Symfony: How to handle common request scope data)

Maybe these tips already helped you a bit. Otherwise let me know more details, where / how you need the data during the rendering.

Sign up to request clarification or add additional context in comments.

4 Comments

I forgot to mention that the dynamic data will be needed in some controllers, too... What are your objections to subscribers?
I dont understand how you would pass the data from your subscriber into your controllers / rendering process. The only way I can imagine is using global vars or something like that. And that's definitely a bad solution.
If you want to access that data in your controllers, too, I would recommend using a central service and injecting the request-stack into it, as described above. Than simply use that service in your controllers and your twig-extension. The calculations for that data can be done on demand if accessed and if needed it can also be cached quite easily.
Ah! Just looked up request_stack and think it's better than my subscriber. Will try that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.