Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

lumen

I've been using Laravel for a long time, and I'm now writing a micro-project using Lumen.

I need to pass some variables to all views. In Laravel I can use the View::share() function in a middleware or in the controller's constructor, but in Lumen there is no View class, and it looks like all view functionality is simply View::make() alias.

Is there a way to share variables to all views?

like image 340
kopaty4 Avatar asked Oct 31 '22 02:10

kopaty4


1 Answers

For performance reasons, Lumen doesn't register facades and service providers the way Laravel does. While the Laravel facades are included with Lumen, only some are aliased (View not being one of them), and only if you uncomment the $app->withFacedes(); line in bootstrap/app.php (you can check the Laravel\Lumen\Application::withFacades method to see which ones). So in order to use other facades such as View, you either need to alias the facade class yourself:

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

Or you can include it with use wherever needed:

use Illuminate\Support\Facades\View;
like image 156
Bogdan Avatar answered Nov 09 '22 14:11

Bogdan