R
R
Rigorbb2015-12-22 12:12:09
symfony
Rigorbb, 2015-12-22 12:12:09

How to share a variable between controllers?

The site is built on widgets (essentially separate controllers) from which the page is composed. Those. when building the page, several actions are twitching for each widget. These actions query the same repository and return the same set of data. Accordingly, there are exactly as many of these requests as there are widgets on the page. How can I share this data between the controllers of these widgets or between the views of these widgets?
I only read about addGlobal in the documentation, but I don’t quite understand where in my case this initialization should be carried out. It also shares the variable in all views, and I would like only for a few. Ideally, find some analogue of view composer laravel.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GrizliK1988, 2015-12-24
@Rigorbb

If you follow your question directly, then you can create a service that will receive data and save it inside (let's just say in private properties), then request data from this service in widgets.
This approach is inconvenient because:
1. You need to save the data before displaying all the widgets.
2. You need to know what data widgets need in a place separate from widgets.
Another way is to wrap the repo in 1 more service, which will pull the methods and cache the received data.

A
Alexander Evgenievich, 2015-12-22
@banderos120

If I understand correctly, maybe you can use include with in twig

A
Alexander, 2015-12-22
@mops1k

Create a service with the necessary functionality and call it where necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question