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

1.3k views Asked by At

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?

2

There are 2 answers

0
Bogdan On

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;
0
qwaz On

The correct way to share data with views in Lumen is:

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

Some of Laravel's functionality that is not explicitly described in Lumen documentation can be accessed with Lumen's app() helper function.