Manage Your Large Laravel Application With Simplicity
Laravel being widely used is one of the reasons it is providing us with a handsome collection of library packages. Laravel is an open-source web framework that is globally popular for the enormous features it offers us. And among those features, one unique feature that makes any web artisan think twice before not using Laravel is its Library Packages. Some well-known Laravel Library packages are Laravel-Auth, Laravel-Breeze, Laravel-Spatie, Laravel-Modules, and more.
Laravel is always there to make our coding experience less hectic. As we know, before building any project some functionalities are common throughout all kinds of applications. Laravel is the framework that sweats on finding out and implementing those common functionalities as library packages. Developers just need to install the required packages and work on the main theme of their application.
One library package of Laravel that manages all the roles and permissions of any Laravel project is Laravel-Spatie. If you want to know more about Laravel and its Spatie package feel free to visit Tired Of Huge And Complex Coding For Permission Handling?
One other package provided to us by Laravel which is the topic we are going to talk about today is Laravel-Modules.
We know when starting a project with Laravel we are given a file structure that follows the MVC architecture. It is very useful and easy to understand the MVC architecture. MVC separates the functionalities from the views.
If we are building a project which registers users, we keep all the codes for registering users in a controller file and only view the form and registration feedback which means whether a user is successfully registered or not. But this is less complex when we are thinking about registering a user. Let us take a step further and think about building an e-commerce website.
When building an e-commerce website we not only think about user authentication but also consider the functionality of adding and removing products, adding selected products to cart, payment option, etcetera etcetera.
If we have a separate controller file for registering users then we also need separate controller files for managing products, handling carts, transferring and receiving payments, user management, bookings, and many more.
Are you being able to imagine the complex file and code structure that will arise if we build a huge application? If not, no need to worry, basically if you fall victim to any bug that appeared in the application you will be constantly looking through your controller files for finding and fixing the bug. Which is an inefficient and very tiring process.
To fix this, Nicolas Widart a freelance web developer specializing in the Laravel framework took a very unique approach. He thought to handle the functionalities of any large applications in modules.
Anyone who is familiar with the concept of OOP or Object-Oriented Programming can understand Laravel-Modules in a jiffy! In short words, the Laravel-Modules package helps create separate Laravel projects inside of the main Laravel project.
The separate Laravel projects that are created by Laravel-Modules basically do what it is told to do by not hampering the main project file. This way if we need to change any part of our project or fix any bugs we just look for the project folder and the controller files that are inside that specific project folder.
As we considered building an e-commerce website earlier, let us consider building a separate project folder for Products management inside our main e-commerce project folder. Now if we have any issue with the products not being able to store or delete we know which controller files we should check on.
To install Laravel-Modules, inside our Laravel project folder we just need to run the following command,
composer require nwidart/laravel-modules
The command above will automatically take care of registering a service provider and alias. To publish the package's configuration file we need to run,
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
To autoload modules we can use ps-4 as this package won't load module classes automatically.
Lastly, we need to run,
If we make a module called Blog, the file structure of that blog module would be,
We can see that Laravel-Modules generates a project folder same as our main project folder's folder structure. It is quite easy to grasp that for this Blog module if we create a database table and do certain migration we have the Database folder. For all the controller files we have an HTTP folder, we also have our Providers folder and a Resources folder that will contain all views.
Now if we have a blog section in our project and create all functionalities of the blog section in this Blog module, any issues with the website's blog section will automatically make us look in the Blog module.
To create a module we need the following command,
php artisan module:make <module-name>
just replace the part with your desired module name.
We can also create multiple modules with a single command,
php artisan module:make Blog User Auth
This will create three separate modules called
If we don't want the traditional file structure that automatically generates resources like controllers, seed classes, service providers we just need to add the
-p flag after the
php artisan module:make Blog --plain
php artisan module:make Blog -p
We have now understood how we can create a module but the question remains how to call them in our main project! Well installing Laravel-Modules will also register custom namespaces,
Lang folder files,
No to get an in-depth taste of how to use this Laravel-Modules package please visit Laravel-Modules
Laravel is always beside us to reduce the burden of complex code. And Laravel-Modules package is a clear example of that statement. Laravel-Modules is pretty handy in managing large applications. It helps us separate our functionalities based on where they are required and gives us a clean file structure.
At UI-Lib we are always focused on keeping our application folder and code more clean and understandable. That is why we prefer Laravel. Some of our products implemented keeping Laravel alongside other options are Stocky - Ultimate Inventory Management System with Pos and Aatrox – TailwindCss Admin Dashboard Template.