i am making a simple website in PHP laravel framework where the top navigation links are being generated dynamically from the database. I am generating the $pages variable in the home controller action and passing to layout file. My code is as below:
public function home()
{
$pages = Page::all();
return View::make('home')->with('pages', $pages);
}
public function login()
{
return View::make('login');
}
But when i try to access the login action, i get the error variable $pages not found since the $pages variable is being accessed in layout file. How can i share the same variable across all the actions in a controller?
I solved the problem by using Laravel's view composer. I made a header.blade.php and passed the $pages variable to it and added following code to my routes.php file.
View::composer('header', function($view){
$pages = Page::all();
$view->with('pages', $pages);
});