|

What is a View Composer in Laravel?

What is a View Composer in Laravel

View Composers provide a very elegant solution to a very specific problem. If you have spent some time building in Laravel, you likely have at some point come across the Undefined variable error exception getting thrown from a view file. So you inspect your methods in your controllers, figure out where you forgot to fetch some specific information, then make the fix, and your error goes away. Many times this happens with partial view files where you load a partial on one section of the site, but perhaps leave it off on another. View Composers fix this problem for you, and in this tutorial, we will examine what causes the problem and how to fix it with View Composers.


Dealing with Partial View Data

We recently covered working with raw sql statements inside of our Eloquent queries. This approach served us well in building up a widget of sorts that displays information about the most active users on our sites. First we set this up in our index view for games, then we set it up for our index view of reviews. Including the partial view file was a piece of cake to include that widget. What about if we want to add that partial to another section of our site? For example, maybe we want to include this partial in our show view for a specific game. Great! That’s easy, you say! We make the needed adjustments to our view file, then check it out in our browser.


resources/views/games/show.blade.php

We visit in our Browser, and what?! Undefined variable: activeusers (View: /home/vagrant/Code/54/resources/views/partials/activeusers.blade.php
error exception undefined variable

Dang Nabbit!!! So we recall, oh yeah that’s right, we need to use that custom query we had put together and make a call to the database so we can populate the $activeusers variable and send it on over to our view file so we stop getting these errors. Ok! Let’s go ahead and update the show() method of our Games Controller.


app/Http/Controllers/GamesController.php

Now we look good!
partial view file now has variable

Fantastic! You see how to include this partial view file anywhere in the app if you like. Tutorial over… Just kidding! Here in lies the problem. We just fixed the issue, but what did we have to do? Think about back to when we first created the most active users widget. At first, we were just going to display this widget in the index view of the Games resource. So easy enough, we included the partial in that view file and updated the index() method of the Games Controller. Then, we thought about it, and said gee “It might be nice to add this widget to the Reviews index view as well”. So we go ahead and include the partial in that view file, then update (read: duplicate) the logic from our Games index() method to our Reviews index() method. Finally in this tutorial, we think, hmm, why don’t we go ahead and add that cool little widget to the show view file for Games. So we add our partial view file to include this widget then test it out in the browser and it grenades. Here is the problem: Any time we want to make use of that partial view file in our app, we have to track down the Controller and method responsible, and keep duplicating the same query over and over, just to make sure one particular variable is populated. There is a better way.


View Composers To The Rescue

If you have been the victim of that mode of thinking we just described, (like myself), you will be happy to learn about View Composers. View composers give us a way to set up a simple listener of sorts, that can be configured to populate a variable automatically for us, so we don’t have to litter all of our controller methods with duplicate code. Here is how we can do that very thing in our example.


app/Providers/AppServiceProvider.php

What this code here does is to set up a listener of sorts that listens for any time the partials.activeusers view file gets loaded in our application. We can see that the second parameter to the composer() method is a callback function. This function gets triggered any time the partials.activeusers view file is loaded. So inside this callback function, we can go ahead and populate that $activeusers variable. With this logic in place, our application could load the partials.activeusers view file in one Controller method, or all of them if you like. No matter what, that partial will be getting access to the data it needs every time. In our case, that is the populated $activeusers variable. So go ahead and update any controller method that is still making calls to $activeusers = User::activeusers(); and remove it. You can also remove the passing of the $activeusers variable to the view files. Our view composer takes care of all of this automatically now.
view composers in action

Everything is working perfect. Our little widget displays without issue on any part of our website that calls for it. We could add it everywhere if we like, and that view composer would still have our back.


What is a View Composer in Laravel Summary

View composers are one of those magical little tricks in Laravel that help to make things just a little bit easier. Could you get by without them? Sure, but once you get the hang of how to use them, you will find that they make perfect sense in many scenarios.