Globals are bad. No matter of what, don't use global variables, don't think about using global variables, always think about how can you not use them in your code and still have all you need to have. Here are some reasons, and there are lot more.
Instead, use Laravel power to help you:
Enforce login on your routes creating groups of authenticated routes:
Route::group(array('before' => 'auth'), function()
{
Route::get('/users/posts', array('as'=>'users.posts.index', 'uses'=>'PostsController@usersPostsIndex'));
});
Now you know that every call to your posts will be authenticated, you can just
class PostsController extends Controller {
public function usersPostsIndex()
{
return View::('users.posts.index')->
with('user', Auth::user());
}
}
In your view you'll just have to
{{$user->email}}
{{$user->first_name . ' ' . $user->last_name}}
{{$user->email}}
{{$user->mobile}}
If you don't want to write code to send a User instance to all your views, use the magic of Laravel's View Composers, adding this to your filters.php or creating a composers.php file:
View::composer(array('users.posts.index','users.posts.edit'), function($view)
{
$view->with('user', Auth::user());
});
And this is now how your views can be used now:
class PostsController extends Controller {
public function usersPostsIndex()
{
return View::('users.posts.index');
}
public function usersPostsEdit()
{
return View::('users.edit.index');
}
}
Because Laravel will automatically inject Auth::user()
in those views as $user
.