I got tired of all those
View::make()->with('foo', $bar) I had in the controllers that I swore I would fix “later”. Well, now it’s later and I finally discovered
The problem: almost every single view I have wants to know stuff about the currently logged-in user to populate menus and other stuff. My temporary solution was to use
with() and pass the User object every single time I created a view:
That got tedious, not to mention redundant. I finally took the time to do a proper read of the documentation, and found
View::share() mentioned in a single sentence. It’s dead simple to use:
The way I like to set things up, I make a
BaseController class that extends Laravel’s own
Controller, and set up various global things there. All other controllers then extend from
BaseController rather than Laravel’s
Controller. (In this case I’m using Sentry to manage user registration/authentication – I can highly recommend it!)
Now I have access to the currently logged in user as
$user in all views, and can access anything from it as normal, such as
$user->email et cetera. Just make sure to check the object first; Sentry returns
null if there’s no user for the session.
If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request – this is how I deal with the User object in Laravel.