P
P
photosho2019-08-09 12:31:12
Laravel
photosho, 2019-08-09 12:31:12

Where is the best place to set a global variable in Laravel?

Where in Laravel 5 can I set a global variable that can be changed in any controller? There is an array initialized with a certain initial value, which is supplemented in some controllers, and then displayed in the main view, at the very top of the page. Where can such a variable be set? Middleware or is there something more suitable?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
NubasLol, 2019-08-09
@photosho

You do a singleton in the provider and that's it. This is how Lara works with a database connection, etc.

public function register()
    {
        $this->app->singleton(Connection::class, function ($app) {
            return new Connection(config('riak'));
        });

}

M
Max, 2019-08-09
@mssmaks

helper

R
RidgeA, 2019-08-09
@RidgeA

best of all, there
is probably another solution somewhere that is not tied to the global state ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question