Laravel / Lumen: View :: share () alternative?

I have been using Laravel for a long time, and now I am writing a microproject using Lumen.

I need to pass some variables to all views. In Laravel, I can use the View::share() function in the middleware or in the controller constructor, but in Lumen there is no View class, and it looks like all the view functions are just an alias of View::make() .

Is there a way to share variables to all views?

+5
source share
2 answers

For performance reasons, Lumen does not register facades and service providers the way Laravel does. While Laravel's facades are included in Lumen, only some of them are smooth ( View not one of them), and only if you uncomment the line $app->withFacedes(); in bootstrap/app.php (you can check Laravel\Lumen\Application::withFacades to find out which ones). Therefore, in order to use other facades, such as View , you need to either independently execute the facade class:

 // "bootstrap/app.php" is a good place to add this class_alias('Illuminate\Support\Facades\View', 'View'); 

Or you can enable it with use where necessary:

 use Illuminate\Support\Facades\View; 
+3
source

The right way to exchange data with views in Lumen:

 app('view')->share(...); 

Some Laravel functions that are not explicitly described in the Lumen documentation can be accessed using the Lumen app() helper function.

0
source

All Articles