Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package
In this post, I will show you how to implement Laravel modules structure in with Nwidart package. This will help us to make our application clean and organized code that is easy to maintain and reusable to any modules within the application.
Some developers recommend implementing a Laravel modules because it comes with own controllers, model, resources and more. That help us not to break our application even if we remove one of our laravel module as long as not a base module that connect with other modules.
Codeanddeploy also using laravel module structures.
When using Laravel modules it will generate with the following:
Config
Console
Database
Entities
Http
Providers
Resources
Routes
Tests
Now let's start installing our Laravel modules.
Step 1: Laravel Installation
If you don't have a Laravel 8 install in your local just run the following command below:
composer create-project --prefer-dist laravel/laravel laravel-modules-example
cd laravel-modules-example
Step 2: Laravel Module Package Installation
Now, let's install our Laravel module package by Nwidart. Run the following command below:
composer require nwidart/laravel-modules
Step 3: Publish Configuration File
Run the following command to publish the configuration file.
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
Step 4: Setup Autoloading
Now, we need to add "Modules\\":
"Modules/",
to your composer under autoload
> psr-4
. See example below:
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Then run the following command after setup above:
composer dump-autoload
Now, we already setup our Laravel module package. Next let's create a module for our Laravel application.
Step 5: Create Module
Syntax of create module command:
php artisan make:module module_name
Then run the following command to create module let's do an example for Posts module.
php artisan make:module posts
After running above commands it will generate our Posts module under Modules folder. See below Laravel module structures:
app/
bootstrap/
vendor/
Modules/
├── Posts/
├── Assets/
├── Config/
├── Console/
├── Database/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── Providers/
├── PostsServiceProvider.php
├── RouteServiceProvider.php
├── Resources/
├── assets/
├── js/
├── app.js
├── sass/
├── app.scss
├── lang/
├── views/
├── Routes/
├── api.php
├── web.php
├── Repositories/
├── Tests/
├── composer.json
├── module.json
├── package.json
├── webpack.mix.js
Now, we successfully generated our Posts
module. Let's test it by running the command below:
php artisan serve
Then run the URL to your browser:
http://127.0.0.1:8000/posts
Then you will see the result below:
For more details about it click Laravel modules.
I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package if you want to download this code.
Happy coding :)
Top comments (4)
The command to generate a module is
php artisan module:make module_name
notphp artisan make:module module_name
This works on Laravel 8+? Because in docs say 5.8
Yes.. still using it in Laravel 9.
I'm also using it with Laravel 10