Problem: Archives Not Global 0:00Now, in the last episode, we focused on setting up the archives here, and that worked great. But there's just one thing. If we click on any other page, that one, or if we create a post, it fails. Because that archives variable, we only set within the post controller, and specifically within the index method. This is the only place we defined it, so the only place it works is for that particular endpoint. So what do we do in these cases? Now your instinct could be, well, we need it in the show view, so we're going to have to do this too, and then pass it through.Now your instinct could be, well, we need it in the show view, so we're going to have to do this too, and then pass it through. And it's true, that will now work, if we click on it. But yeah, clearly, this is not what we want. It sounds like, for every single page in our website where there will be a sidebar, which is essentially everywhere, we want to make sure that the archives are loaded. Okay, for that, we can use a Vue Composer. So I'm going to bring this back to how it was, nice and simple. And then here, well, as a first step actually, let's do a little bit of refactoring. I don't want to do this whole query. Refactor Archives Query 0:59And then here, well, as a first step actually, let's do a little bit of refactoring. I don't want to do this whole query. So instead, what is this? Well, we are fetching the archives. So maybe I could just call a method named archives. Alright, so let's go to our post model, and a simple archives method will do the trick. And that should be static. Alright, now I can paste this in, and we will return static, and then our query. Okay, let's come back and give that a refresh. And we still get the same thing, but we've taken this slightly more complicated query,Okay, let's come back and give that a refresh. And we still get the same thing, but we've taken this slightly more complicated query, and we've hidden it behind a dedicated method. So that's cleaner. But next, I want this out of here as well, because it should be available everywhere. So let's test it. We refresh. It no longer works. Alright, we're going to place this within a service provider. And in a follow-up episode, we're going to talk entirely about service providers. Using App Service Provider 1:48Alright, we're going to place this within a service provider. And in a follow-up episode, we're going to talk entirely about service providers. From my experiences, they're a little scary at first until you understand them, and then it makes perfect sense. So here's a list of our providers, and you'll see a handful out of the box. Let's focus on the app service provider, which is almost like a dedicated service provider specifically for you to get started. You're going to see two different methods here. One is register, where we may bind things into the service container. I'm going to teach you all about that soon.One is register, where we may bind things into the service container. I'm going to teach you all about that soon. But next, once all of these service providers, which are sort of like the building blocks of Laravel, once they've all been registered, Laravel will then filter through again and call this boot method on each. So think of this as a place where you can perform any action or logic with the assumption that the framework is fully loaded. So we're going to register a view composer. We can do that with the view facade. Or like you've learned, there's also a view helper function that will do the same thing. Register View Composer 2:41We can do that with the view facade. Or like you've learned, there's also a view helper function that will do the same thing. Now the way a view composer works is we can hook into when any view is loaded. Think of it almost like a callback. So for example, if we go to our layout slash master file and we scroll down, here's a section where we load the sidebar. And we basically decided anywhere that we have a sidebar, we need to make sure that we have an archives variable available. Okay, well that's what a view composer is for. Let's listen for when the layouts.sidebar is loaded.Okay, well that's what a view composer is for. Let's listen for when the layouts.sidebar is loaded. And then I want to register a callback function where we can bind anything to that view. Now here we're passing a closure, but you could also pass a class path. And that way you could create a dedicated class for your composer. But I think often you'll find a simple callback function here will do the trick. Now I'm going to add a variable called archives. And we'll make that equal to, well how about this, at post, give me the archives. Simple enough, right? And you'll notice the syntax, it's the exact same thing as right here, like if we fetchSimple enough, right? And you'll notice the syntax, it's the exact same thing as right here, like if we fetch a value. Well I showed you that you could do something like this, right? But we also discussed how you could say with posts equals posts. This is the exact same thing that we're doing here. Alright, so I'm going to bring that back a few clicks. And I think we're all set to go. So let's go back to Chrome, give it a refresh, and whoops, it looks like I forgot to make that global. Verify Archives Everywhere 4:09So let's go back to Chrome, give it a refresh, and whoops, it looks like I forgot to make that global. Alright, one more time, and there we go. We have our archives. And now for any page we click on, we will have access to that, as you can see right there. Okay, so that's view composers in a nutshell. Very very simple things. Register a composer where the first argument is the name of the view that is loaded. So this has to be very specific.Register a composer where the first argument is the name of the view that is loaded. So this has to be very specific. I can't just call this sidebar because then it won't work. It has to be the name, if we go back to layout slash master, that you are loading. So in this case, layouts dot sidebar is the name. So hook into that, then I want to bind something to the view, specifically a variable called archives, and it will be equal to what is returned from this. And that's all there is to it. Now take a look. Now back in our post controller, once again, everything is nice and simple.Now take a look. Now back in our post controller, once again, everything is nice and simple. Just a few lines of code per method.