Laravel 5, View :: Share

I am trying to do view::share('current_user', Auth::User()); but in laravel 5 I can’t find where to do this, in L4 you can do it in baseController, but this no longer exists.

grt glenn

+6
source share
5 answers

First you can create your own BaseController and extend it in other controllers.

Secondly, you can use Auth:user() directly in the view, you do not need to assign anything to the view.

For other uses, you can go to app/Providers/App/ServiceProvider.php and in the boot method View::share('current_user', Auth::User()); but or of course you need to import namespaces first:

 use View; use Auth; 

because this file is in the App\Providers namespace

+11
source

I am using Laravel 5.0.28, view::share('current_user', Auth::User()) no longer works because this problem is https://github.com/laravel/framework/issues/6130

Instead, I create a new service provider using the wizard instead.

 php artisan make:provider ComposerServiceProvider 

Then add ComposerServiceProvider to the config/app.php providers

 //... 'providers' => [ //... 'App\Providers\ComposerServiceProvider', ] //... 

Then open the app/Providers/ComposerServiceProvider.php that has just been created, add the following inside the boot method

 /** * Bootstrap the application services. * * @return void */ public function boot() { View::composer('*', function($view) { $view->with('current_user', Auth::user()); }); } 

Finally, import View and Auth facade

 use Auth, View; 

For more information see http://laravel.com/docs/5.0/views#view-composers

+8
source

This can help:

 App::booted(function() { View::share('current_user', Auth::user()); }); 
+4
source

I tried this, put it in the application / Providers just didn't work. An alternative way is to create a global middleware and put View :: share ('currentUser', Auth :: user ()); there is.

+1
source

Laravel 5 uses the same method as laravel 4:

 View::share('current_user', Auth::User()); 

or using the view helper:

 view()->share('current_user', Auth::User()); 

See http://laravel.com/docs/5.0/views

+1
source

Source: https://habr.com/ru/post/976482/


All Articles