Recently in one of my project we have an admin panel crated with Laravel. There was a requirement to setup global object that can be accessed in all controllers, views, models etc. Basically it was logged in user object with additional information so we can access details of user in anywhere. Here in this blog I am going to explain how to do this.
First open app -> config -> filters.php file and add following code to it.
App::before(function($request)
{
// Singleton (global) object
App::singleton(‘app’, function(){
$app = new stdClass;
if (Auth::check()) {
// Put your User object in $app->user
$app->user = Auth::User();
$app->user_access = UserAccess::getAccess($app->user);
}
return $app;
});
$app = App::make(‘app’);
View::share(‘app’, $app);
});