Tooling for Modular Laravel0:00
Hey there, welcome back to a new lesson. Let's take a break from coding a little bit and talk about tooling. One of the reasons people love Laravel, myself included, is that it does a lot of the heavy lifting for you, and that includes giving you access to commands that scaffold things, set up things for you, create classes, files, etc. When you go with a modular structure, or really any structure that does not follow the standard structure, you'll lose access to some of those, or you can use them and then move things around. But again, you don't have full access to the easiness that Laravel provides. I can say that I personally do not feel that's a big drawback because I'm used to creating
Introducing Laravel IDEA0:32
But again, you don't have full access to the easiness that Laravel provides. I can say that I personally do not feel that's a big drawback because I'm used to creating classes and files and using PHP Storm shortcuts very well, so it doesn't affect me very much. But thankfully, there are tools that allow you to regain some of that easiness. Let's take a look at them. I want to start with Laravel IDEA. Laravel IDEA is a paid plugin for PHP Storm, and it does a lot of things when it comes to Laravel. I don't have time to talk about all of them on this video, but it really does a lot. And one of the things it does is allowing you to create models and migrations and all.
Configuring Module Settings1:04
I don't have time to talk about all of them on this video, but it really does a lot. And one of the things it does is allowing you to create models and migrations and all of that through PHPStorm. So let's take a look at that. All right, so if you have Laravel IDEA installed, you can go into Laravel, Laravel IDEA settings, go into the module system section, and you have a bunch of options to choose from. First, let's go with simple directory modules. We can specify the root directory namespace and the path. This is not what we have, so let's update this for our needs. Well, our base namespace is modules, and our base path is modules.
Generating Module Code1:32
This is not what we have, so let's update this for our needs. Well, our base namespace is modules, and our base path is modules. So let's update this, save it. And if I were to go here and go to code generation, for example, let's create a model. Create model. Let's say that we want to call a TestItem, and we can pick the module. So this is pretty cool. We want this to go under payment. We can tweak our migration. So let's say name as a field.
We can tweak our migration. So let's say name as a field. We can instruct Laravel IDEA to create a bunch of things. Let's go with just a migration for now and click OK. So as you can see, we got a test item under modules, payment, models, and we also got a migration. But the migration just went under database/migrations, the root namespace. So this isn't exactly what we want. It can, however, save you a bunch of time, because if you were to create anything else like a resource or a controller, it will use the Laravel default structure within the module.
Exploring Module Packages2:27
It can, however, save you a bunch of time, because if you were to create anything else like a resource or a controller, it will use the Laravel default structure within the module. So if that's the structure you're going with, it works great. For example, let's go with a ProductController. And as you can see, we're under Order\Http\Controllers. So if you're following the default Laravel structure within your modules, that strategy works great. The second option that we have is the Laravel Modules package. This is probably the de facto package for modularization within the Laravel ecosystem. It is widely used, and it has its own conventions.
This is probably the de facto package for modularization within the Laravel ecosystem. It is widely used, and it has its own conventions. So you could also use this package to build your modules. As you can see, its base structure is very similar to what we built. I'm not going to go into the documentation because it is very extensive, but that's a fantastic option for you to leverage modularization with. And if we go into Laravel IDEA, let's go into the settings. It also supports that package that we can go and select the Laravel modules package, just configure our base namespace and our base path. And it's actually modules in lowercase.
just configure our base namespace and our base path. And it's actually modules in lowercase. And that's it. It will follow that package's structure. Another option is this package called modular for internazi. This is a great package. It does pretty much a lot of the heavy lifting for you. It's not as widely used as Laravel modules. It hasn't been around that long. But as you can see from the documentation, it has a walkthrough video.
It hasn't been around that long. But as you can see from the documentation, it has a walkthrough video. It's really well made. That's also an option. And it does some things for us. For example, it has a slightly different structure. So you have an app modules folder, you have your modules, you have to include a composer.json file, and then everything goes into a source directory. So that's an option as well. If we scroll down a little bit, you can see that it does some heavy lifting for us, like
So that's an option as well. If we scroll down a little bit, you can see that it does some heavy lifting for us, like auto-rectoring commands, auto-discovering Blade components, and also event listeners. So if you want to leverage some automation, this package is also a good option. And on the Laravel IDEA side of things, we can go into module system and also select directory modules as composer.json packages. I didn't talk about package development because we're not doing package development, so it's not useful for us. But if we select this, we have pretty much the same structure. We can select the source path, we can select the root directory path, and then the composer.json
Choosing a Modular Approach4:44
But if we select this, we have pretty much the same structure. We can select the source path, we can select the root directory path, and then the composer.json file path. So that's an option as well. As you can see, we have plenty of options to choose from to use a modularization structure. And if you're not happy with any of them, you can also build your own. It's not that complicated. You just have to instruct Laravel where to load things from. With that said, I do suggest that you look at all of those options. I'm sure that at least one of them is going to suit your needs very well.
With that said, I do suggest that you look at all of those options. I'm sure that at least one of them is going to suit your needs very well. Maybe not entirely, but at least save you some time. So that's pretty much all there is to this video. As usual, I hope you enjoyed this video, and I'll see you in the next lesson. Bye-bye.
