Reputation: 5353
I have a view called admin.users. In that view I include header and footer using @include directive. I write all my views-including stuff in routes.php, so for admin.users it is:
Route::get('users', function() {
// ...
return View::make('admin.users')->with('num', $usersNum);
}
And in users.blade.php:
@include('admin.partials.header')
// ....
@include('admin.partials.footer')
Is it possible to pass "users num" to header view in order to show that variable? And is it a good practice the way I'm combining views, because I read about controller layouts but actually I decided to have only rest controllers while I include view only in routes.php (like load static markup and after that communicate with server by ajax)
Upvotes: 1
Views: 4357
Reputation: 3918
Have a look at 'ViewComposers' (http://laravel.com/docs/responses#view-composers). This is a great way to share data with your views and keep your routes file clean.
In global.php (or any other place really) add:
View::composer('admin.users', function($view)
{
// Do your $usersNum logic here
...
$view->with('num', $usersNum);
});
If at some point you want this data to be available in admin.dashboard as well, just rewrite to:
View::composer(array('admin.users', 'admin.dashboard'), function($view)
Upvotes: 3
Reputation: 8672
As I said in my comment (and what seems to have fixed your issue) you should create an admin.template
view with your header and footer in it, and start your views with @extends('admin.template')
.
Upvotes: 0