Reputation: 4186
I'm building a simple diary app at the moment using Laravel 4, and I would like to create a sidebar and header that is shared across all admin views. The header will require user data to be displayed, while the side bar will contain links to the various sections of the app.
I'm really unsure how to go about this an have looked at View Composers but I'm not sure how to implement them properly, or if this is even what they are intended for.
If you can provide me with any clear documentation on this or any other resources that would be awesome. Thanks.
Gareth
Upvotes: 1
Views: 1473
Reputation: 33078
For sites that aren't that complicated, I like to just create an easy template using blade.
//file: template.blade.php
@include('includes/header')
@include('includes/sidebar')
@include($view)
@include('includes/footer')
Obviously, I'll have the header.blade.php
, sidebar.blade.php
, and footer.blade.php
files in the views/includes folder
.
Then, in the controller, it's as easy as...
$view = 'exampleView';
return View::make('template')->with('view', $view);
Additionally, all the variables you pass into the template view will also be made available to each of the includes called in that template.
Upvotes: 0
Reputation: 4186
I have managed to accomplish what I wanted to do via a View Composer but I would like to know your thoughts on it and whether it would be considered best practice or a good solution.
// View::composer for layouts.admin in my routes.php file
View::composer('layouts.admin', function($view)
{
$user = AdminController::get();
$view->with('user', $user);
});
// public static method get() in AdminController
public static function get()
{
$userGreeting = Auth::user()->greetings()->orderBy(DB::raw('RAND()'))->first();
$user = new User;
$user->first_name = Auth::user()->first_name;
$user->greeting = $userGreeting->greeting;
return $user;
}
// layouts.admin view
{{ Html::link('logout', 'Logout') }}
<p>Hey {{ $user->first_name }}</p>
<p>{{ $user->greeting }}</p>
Upvotes: 1