V
V
Vasyl Fomin2018-05-19 14:45:43
Laravel
Vasyl Fomin, 2018-05-19 14:45:43

What is the best approach to use to pass a variable to all blades in Laravel?

On the site, for example, there is a menu in which you need to display categories and a footer in which you need to display a list of cities. At the same time, you should not be tied to the name of the frontend template on the backend, since it should be possible to display this data in other parts of the site template without rewriting/adding the backend code later.
I know of two possible ways to implement the above in Laravel:
1. 2.View::share('key', 'value');

View::composer('*', function ($view) {
    //
});

But somehow it does not come out "elegantly", and the use of global variables is not a good idea, well, it turns out that the data request needs to be written in the service provider class (or view composer). But I don't know of any other way.
Are there any recommendations or approaches for implementing such functionality?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-05-19
@fomvasss

Don't like "global" variables, use option #2.
If you don't like requests in service providers, make them in some third-party classes, and call this class in the service provider.
Or let the service provider refer to some class and get all the necessary variables for compositing.
Option number 3: subscribe to compositing events on your own and build your own logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question