Make your Laravel project Modular
Build each part of your project as a module to manage your large scale project.
When your project becomes bigger and there is a lot of files, or if you are planned to make a large scale project then you find it difficult to manage, making parts of your project into modules is the one of solution to overcome the problem.
With modules you can grouping each section such as Blog into separate package. That will make it easier for you to do maintenance on things related to the Blog.
Install and Setup
To create a module in Laravel we need package from nwidart/laravel-modules. nwidart/laravel-modules
is a Laravel package which was created to manage your large Laravel app using modules. A module is like a Laravel package, it has some views, controllers or models. This package is supported and tested in Laravel 5. To install the packages run following command :
composer require nwidart/laravel-modules
After completion of installation the package will automatically register a service provider and alias.
Optionally, you can publish the package’s configuration file by running:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
By default, the module classes are not loaded automatically. You can autoload your modules using psr-4
in composer.json
file. For example :
Then run composer dump-autoload
command afterwards.
Create a Module
Now we can create our modules in our project. To do this run following command :
php artisan module:make <module_name>
# For Example
php artisan module:make Blog
It is possible to create multiple modules in 1 command with command below :
php artisan module:make Blog User Post
It will generate a Blog, User and Post into Modules folder of your project, like picture below.
Now your project has already modular, and you will be able to create other modules easily.
Thanks for read my post, next I will share about create other things in modules such as model, seeder, controller etc. If you want to discuss or have any question please feel free to leave a comment. Share with others if you found this post useful. See ya