Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5, View::Share

I'm trying to do a view::share('current_user', Auth::User()); but in laravel 5 i can't find where to do this, in L4 you could do this in the baseController, but that one doesn't exists anymore.

grt Glenn

like image 214
Kiwi Avatar asked Oct 09 '14 10:10

Kiwi


1 Answers

I am using Laravel 5.0.28, view::share('current_user', Auth::User()) doesn't work anymore because this issue https://github.com/laravel/framework/issues/6130

What I do instead is, first create a new service provider using artisan.

php artisan make:provider ComposerServiceProvider

Then add ComposerServiceProvider to config/app.php providers array

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

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

/**
 * 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

like image 93
xwlee Avatar answered Sep 16 '22 12:09

xwlee