Locating Breeze Provider0:00
In the last episode, we installed Laravel, we pulled in Laravel Breeze, and we stopped just short of running this command. So let's do this. Let's open this in my editor. And now, yeah, I mean, we could go into the vendor directory, find Laravel, find Breeze, look for the provider. But as a general guideline, any time you pull in a Laravel package, the service provider is going to be package name service provider. So you can usually guess it, which means I would normally just say, okay, I'm using Laravel Breeze.
So you can usually guess it, which means I would normally just say, okay, I'm using Laravel Breeze. Let's look for BreezeServiceProvider. Oh, there it is. Now, as you may know, a service provider is sort of like the bootstrapper for a package. It's sort of like a handshake to make your package known to the current Laravel project. Now if we have a look here, there's nothing in register, which means there are no bindings being added to the container. But as part of the bootstrapper, we can see, okay, Laravel adds a new artisan command called install command.
But as part of the bootstrapper, we can see, okay, Laravel adds a new artisan command called install command. And that corresponds here. So for example, if we have a look here, let's just run that and grep it to Breeze. There we go. There's the command. And that's being added because we register it here. So if I were to comment that out and run it again, of course, we're not going to see anything at all. Okay.
Deferred Providers Explained1:19
at all. Okay. So again, we'll take a look at the install command in a moment. First, I want to point your attention here. This kind of confused me in the early days when I was learning about providers. What does the provides method actually do? I get that it says here, get the services provided by the provider, but why? And why does this correspond to that? And then if I scroll up, I also see this. So yeah, if you're not fluent, it can be a little confusing.
Only resolve this and load it if a relevant service is requested. And if it's not, then we can improve performance by not doing anything at all. And that's exactly what this allows for. So you'll see in our case, all we have is an artisan command, which means, you know, we can just, we can delay that. Let's load this provider at the last possible minute. So now the question is, well, how do you tell Laravel what the last possible minute to load it is? And that's where the provides method comes into play. These are the services provided by the provider.
And that's where the provides method comes into play. These are the services provided by the provider. And that means if you have the provides method and you implement this interface, only when we need to will Laravel resolve this provider and run it. So it's just a way to improve performance a bit. So for example, if we scroll back up, maybe you also had something here in your provider where you register a singleton for some kind of Foo class. You get the idea. Well, you could still defer that. You just need to declare foo as one of the services.
Artisan Command Basics3:10
Well, you could still defer that. You just need to declare foo as one of the services. So again, Laravel will know that only when you attempt to resolve foo will Laravel actually load this service provider. All right. So just a small thing to be aware of. But yeah, in the case of LearningBreeze, this provider is very simple. The only thing it does is it registers an install command. So this is a standard artisan command. You can create them on your own by saying php artisan make:command like this.
So this is a standard artisan command. You can create them on your own by saying php artisan make command like this. And if we were to take a look at this, it'll be in my, by the way, it'll be on the app/Console directory and you'll see it here. Anyways, notice it's the exact same thing. It's an artisan command. Let's go ahead and delete that. Anyways, here is the signature and the handle method here will be executed as soon as I run the command from the console. So as soon as I run this, it'll hit the handle method.
Updating package.json Dependencies4:06
run the command from the console. So as soon as I run this, it'll hit the handle method. All right. Let's have a look together. I've never seen this before. First, it updates node packages. So it looks like it's updating your package.json file. So the package.json for your local project right down, excuse me, right down here. It looks like it needs to update this to reference whatever node specific dependencies you require. So you'll see, looks like he's added a helper function where you get the current packages.
It looks like it needs to update this to reference whatever node specific dependencies you require. So you'll see, looks like he's added a helper function where you get the current packages and then you, whenever you add two arrays together in php, it's basically a way to merge them if you didn't know that. So for example, I could say foo is bar plus baz is buzz. It's just an array_merge basically. And if there are any duplicate keys here, this one will take precedence. Okay. So it looks like for Breeze to work, it pulls in Tailwind CSS forms. That's special styling specifically for your forms.
Let's figure out how it updates the node packages. So let's scroll through here. Update the package.json file. All right. So again, yeah, it's checking to see. Is there a package.json file in the root of your project? If there's not, it doesn't do anything at all. Otherwise, it figures out are we updating your dev dependencies or your dependencies? In this case, dev is set to true. So we're going to update this section here.
In this case, dev is set to true. So we're going to update this section here. All right. Let's figure out how. So it does file_get_contents. So it grabs the contents of this file and it decodes it into JSON. So that means if I were to die and dump the packages right there. And let's go ahead and run php artisan breeze:install. Yeah. So you can see it loaded the contents of package.json, it converted it to an array, and this is what
Yeah. So you can see it loaded the contents of package.json, it converted it to an array, and this is what we get. Okay. Next, it's saying packages, and we're going to go into devDependencies right here, equals this callback function. Now this idea of passing a callable to a function is super common across all of Laravel. It's surprisingly not used that often, but it's a really nice way to pass in configurability. So anyways, let's go back. Where were we?
So anyways, let's go back. Where were we? Okay. So Laravel passes a function here that will accept packages, and then it returns the new packages array that presumably it wants to use. So let's have a look. It will call that function that we just looked at. Yeah. It's going to pass in whatever is here. So it's going to send this array to that function.
It's going to pass in whatever is here. So it's going to send this array to that function. So our function here is just going to say, okay, take this and merge it with this and then return it. So let's see. All right. So now we've updated packages configuration key, and then we sort it. And if you're not familiar, ksort sorts it according to the key of the array in alphabetical order. So if I were to, again, die and dump, what do we have here?
order. So if I were to, again, die and dump, what do we have here? packages configuration key. Let's run it again. So now we have a sorted list of the dependencies that are included with Laravel out of the box as well as the dependencies that Breeze requires. Okay. So now I assume we have an updated array of dev dependencies. The final step, and there it is, the final step is to write the package.json, and the new contents will be an encoded version of that packages array.
The final step, and there it is, the final step is to write the package.json, and the new contents will be an encoded version of that packages array. And that's all that happens there. So let's do this. I'm going to copy this, and then we'll just return done. And let's just see what happens. So excuse me. Notice right now we have axios, mix, lodash, and postcss. We run it again, and now it updated it. Okay.
Copying Stubs and Routes8:12
We run it again, and now it updated it. Okay. That's simple enough. Nothing too fancy happening there. But I'm going to bring it back to what we had before and move on to the next step. After it updates package.json, let's see, it's making sure we have a bunch of directories here. So it's looking into here, and it's making sure we have some kind of auth folder. Now ensure probably creates it, yeah, if it doesn't exist. So we're saying, all right, if we don't have that auth directory in controllers, then go
Now ensure probably creates it, yeah, if it doesn't exist. So we're saying, all right, if we don't have that auth directory in controllers, then go ahead and create one yourself. Then it's going to copy one of the stubs from Breeze to that auth folder. So let's have a look here. Let's go down to, hmm, Laravel, Breeze. It goes into the stubs directory. It goes into app, HTTP, controllers, and there we go. Okay. So we are using the copyDirectory method on the FileSystem class to copy this full
Okay. So we are using the copyDirectory method on the File class to copy this full directory to your local project. So it's going to go right here. Okay. So once again, we're just figuring out how this stuff works. So we run it again. It will update package.json, and then it will copy that auth directory over to your project so that you are now in control of these files. And that's an important thing with Breeze.
It will make sure you have a views directory, auth, layouts, and components directories. And then it will copy all of the stubs for Breeze over to those folders. Next, it copies a dashboard view. And then we're going to have a view components directory. So it's just setting up your project in the proper shape. And this is why you would want to run this command at the beginning of your project. You'll see even right here, it is overriding routes/web with this stub. So let's look for it. stubs/routes/web. And you'll see normally you'd have something like, I can't delete that, but normally you
Stubs routes/web.php. And you'll see normally you'd have something like, I can't delete that, but normally you would have just this section here. But the stub adds an endpoint for the dashboard as well as these auth specific routes for registration, login, forgot password. So now you're starting to see if you were to create your own package for things like this, how you might go about it. Let's come on back. So again, I don't want to go over all of these. It's the same thing.
Search-and-Replace Helpers10:50
So again, I don't want to go over all of these. It's the same thing. But I do want to take a look at this section. This catches my eye. Replace in file. So it looks like in views/welcome.blade.php, we are replacing /home with /dashboard. So I assume this is a helper function they added to do a quick search and replace. So let's have a look. views/welcome.blade.php. And we're going to replace /home with /dashboard.
Views welcome. And we're going to replace /home with /dashboard. Yeah, so I assume this is just a quick way to overwrite the default stub. So let's figure out how that works. We're going to click through. Very simple. And situations like this always start at the end. So we're going to replace what you're searching for with what you're replacing for using this string. And then ultimately, we're going to write that replacement back to the file in question.
string. And then ultimately, we're going to write that replacement back to the file in question. Okay, so that's easy enough, right? All we're saying here is get the content. So get this file. And then we're going to look for /home. And you're going to replace it with /dashboard, I think. And once you've made that replacement, you'll have an updated version of this file, where we write it to views/welcome. Simple helper function.
we write it to views/welcome. Simple helper function. So let's come back. And it looks like it does that twice here. And then the final thing in RouteServiceProvider. Let's see what that does. So if you're wondering, like, why do I need to do it? You don't need to do this. Normally, just install Breeze and be happy that everything works. The point of series like this is to learn how things are wired together to help improve
Normally, just install Breeze and be happy that everything works. The point of series like this is to learn how things are wired together to help improve your own projects and your own workflow. It's the only reason we're doing this. I don't expect newcomers to dig behind the scenes of every package and tool they use. It's not necessary. It's only for learning. So anyways, in this last one, we look for /home. Yeah. And we are updating the home route directly within your ServiceProvider.
Yeah. And we are updating the home route directly within your ServiceProvider. So that'll ultimately point to your dashboard when everything is done. OK. And then finally, it gives you some output. It tells you to install your dependencies and you're all set to go. So let's go ahead and do this officially. OK. We will copy this. This installs all of your node dependencies.
We will copy this. This installs all of your node dependencies. Here we go. We're using Laravel Mix 6.0.11, and I think we now know how the install command works. So let's go ahead and run Breeze.test, and we'll now see the login and registration links. Perfect. In the next episode, let's keep learning.
