Why Use Composer 0:00Welcome back. So I'm going to go into my core bootstrap file, and you'll see that we are manually requiring in all these files that we need. And so far, this is still, relatively speaking, a very basic project, but we keep finding ourselves having to require more and more files. And this is really a pain. Every time I want to new up a class, I don't want to have to require the file, I just want it to automatically load. So PHP does provide support for this, and in the past, everyone kind of did it in their own way, and used their own conventions. But then, a number of years ago, a dependency manager called Composer was released that kind of normalized everything. And even better, it gave you a very fast and efficient way to pull in packages built by the rest of the community. Maybe you need to modify an image, or create a thumbnail. Well, you could use Composer to pull that in. But even beside that, we're not going Installing Composer 0:45packages built by the rest of the community. Maybe you need to modify an image, or create a thumbnail. Well, you could use Composer to pull that in. But even beside that, we're not going to review any of that stuff in this series. But one nice thing that Composer does provide is, like I said, an autoloader out of the box. Let's go to Chrome, and you're going to visit getcomposer.org. Now, by the way, if you're thinking to yourself, oh, I don't want to worry about this, I don't need it. Trust me, it's the industry standard at this point. You want to learn Composer. And it's not very hard. We've covered it multiple times on Laracast. So we'll grab this really quick. Go to Download, and here you'll get a handful of links to run within the terminal. Once you're done, you should be able to run Composer, and you'll get something like this. Excellent. Okay, so let's go back to Sublime, and I'm going to create a new Composer.json file. Configuring Autoloading 1:26Once you're done, you should be able to run Composer, and you'll get something like this. Excellent. Okay, so let's go back to Sublime, and I'm going to create a new Composer.json file. And I'm going to specify how I want Composer to autoload my files. Now, specifically, I'm going to provide a class map, which means load every single class within the path I give you. So, for example, if we wanted to autoload all of the controllers, we could do something like this. Now, it'll find any classes within this directory and autoload them. Or, for now, in real life, you're going to end up using a slightly different convention called PSR-4. But for our needs, we just want to autoload every class. So I will use this to represent the current directory. Autoload any classes within here. Okay, so we have the beginnings of our Composer.json file set up. I'm now going to say Composer install. Now, like I said, if we had anythe current directory. Autoload any classes within here. Okay, so we have the beginnings of our Composer.json file set up. I'm now going to say Composer install. Now, like I said, if we had any dependencies specified, it would pull those in for you. However, in our case, we don't have any declared, so it'll simply do the initial install. And as part of that, notice we do get an autoloader. And if we dig into class map, there we go. So notice it has a reference to the name of the class, like connection right here, as well as a path to where the file is located. So now Composer understands how to autoload these files and where they're located. So that means if we were to come back to our bootstrap file, and if we were to comment these out, we'll take a look. I'm going to come back, and we want it to still work, but no, we can't find the query builder. So this is a very important thing you need to understand about Composer. In order to make sure it works, you have Requiring Vendor Autoloader 2:58to come back, and we want it to still work, but no, we can't find the query builder. So this is a very important thing you need to understand about Composer. In order to make sure it works, you have to require this one file. So you have to require the autoloader, and then basically that's the only file you'll need to worry about. Okay, so generally you'll do that sort of like at the entry point to your application. So maybe somewhere like here. Require vendor autoload.php. Just remember that when you use Composer within your entry point, make sure you require the autoloader. Okay, so if we switch back, these are commented out. We're not having to require them at all, but if I come back and refresh, everything's working, and that's because we have an autoloader working for us. Very cool. So we could get rid of all of this junk here, and we're in good shape. So yeah, there's actually quite a bit more to Composer, but I don't want you to worry about Introducing DI Container 3:45working for us. Very cool. So we could get rid of all of this junk here, and we're in good shape. So yeah, there's actually quite a bit more to Composer, but I don't want you to worry about it too much just yet. We have other series at Laracast that will show you the ins and outs of it. So for now, even if you only used it for its autoloading capabilities, that's a really good habit to get into. Okay, so now that we've cleaned up our bootstrap file just a bit, I next want to point our attention to this, where we're sort of storing these dependencies, like our configuration file and the query builder, within an array. But instead, let's set up a dedicated class for that, so that we can bind dependencies into it, and then, when we need to, resolve dependencies out of it. We call this a simple dependency injection container.and then, when we need to, resolve dependencies out of it. We call this a simple dependency injection container.