how to use Laravel’s named views and view composers

This recipe will show how to use Laravel’s named views and view composers to simplify some
of our routes’ code.

Getting ready

For this recipe, we’ll be using the code created in the Creating menus in Laravel recipe.
We’ll also need the assets package installed in the Adding assets recipe.

How to do it…

To complete this recipe, follow these steps:
1. In the routes.php file, add a file named view, and add the following code to it:
View::name(‘menu-layout’, ‘layout’);
2. In routes.php, add a view composer as follows:
View::composer(‘menu-layout’, function($view)
{
Asset::add(‘bootstrap-css’,
‘http://netdna.bootstrapcdn.com/twitterbootstrap/
2.2.2/css/bootstrap-combined.min.css’);
$view->nest(‘menu’, ‘menu-menu’);
$view->with(‘page_title’, ‘View Composer Title’);
});
3. In routes.php, update the menu routes as follows:
Route::get(‘menu-one’, function()
{
return View::of(‘layout’)->nest(‘content’, ‘menu-one’);
});

Route::get(‘menu-two’, function()
{
return View::of(‘layout’)->nest(‘content’, ‘menu-two’);
});
Route::get(‘menu-three’, function()
{
return View::of(‘layout’)->nest(‘content’, ‘menu-three’);
});

4. In the views directory, update the menu-layout.php file with the following code:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title><?= $page_title ?></title>
<?= Asset::styles() ?>
<style>
#container {
width: 1024px;
margin: 0 auto;
border-left: 2px solid #ddd;
border-right: 2px solid #ddd;
padding: 20px;
}
#menu { padding: 0 }
#menu li {
display: inline-block;
border: 1px solid #ddf;
border-radius: 6px;
margin-right: 12px;
padding: 4px 12px;
}
#menu li a {
text-decoration: none;
color: #069;
}
#menu li a:hover { text-decoration: underline }
#menu li.active { background: #069 }
#menu li.active a { color: #fff }
</style>

</head>
<body>
<div id=”container”>
<?= $menu ?>
<?= $content ?>
</div>
</body>
</html>
5. In the browser, go to http://{your-server}/menu-one (where your-server is
our URL) and click through the menu links.

How it works…

We begin the recipe by creating a name for one of our views. If we have views with long or
complicated filenames or directory structures, this will allow us to create a simple alias in our
routes. It will also let us change our view filename in the future; additionally, if we’re using it in
more than one place, we only need to change one line.
Next, we create a view composer. Any code in the composer will automatically be called when
you create the view. In our example, we’re including three things every time our view is created:
an asset containing a Bootstrap CSS file, a nested view, and a variable to pass to the view.
For our three routes, instead of View::make(‘menu-layout’), we’ll use the name we
created, call View::of(‘layout’), and nest it in our content. Since our layout view has
a composer, it will automatically nest in our menu, add the CSS, and pass in a page title.

Leave a Reply

Your email address will not be published. Required fields are marked *