Answer the question
In order to leave comments, you need to log in
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
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.
If I understand correctly, maybe you can use include with in twig
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question