aln447
aln447

Reputation: 1011

Symfony 3, such a thing as a global controller?

I'm sorry for the code-less-ness of this question but is there like a global controller that fires for every website page?

Like for instance on 90% of my websites content I want to have a "recent comments" feed printing all the latest comments from the database.

In which file/function could I define such functionality without having to print it in every controller?

Upvotes: 1

Views: 501

Answers (2)

Don Omondi
Don Omondi

Reputation: 946

I think your question is just a little bit ambiguous, because if what you're looking for is a global controller, then request listeners are probably the way to go, but in your example of why you want to do it, request listeners would mean a DB query(s) on every request, I would highly recommended using ESI includes for this http://symfony.com/doc/current/http_cache/esi.html to because if you think of it, what you're trying really has little to do with Symfony per say and more with presentation so try and solve it from that point of view.

Upvotes: 0

Stepashka
Stepashka

Reputation: 2698

There is no such thing as a global controller in Symfony. preDispatch and postDispatch are gone long time ago.

Nowadays everything is done with events.

If you want to run some code before the controller is executed you should add a listener to kernel.controller event. See details about events in Symfony documentation.

This is the detailed explanation how to do it.

But...

In your case I don't think it is needed to do this. Instead you can use Twig Etensions. Set up the extension as a service (with all dependencies). Call it in the place where you need to show "recent comments". I think that in your case Twig Functions fits the best.

Upvotes: 3

Related Questions