Modular Example Implementation in Laravel 8
1. Search in GitHub: Laravel Module, select the first result: NWidArt / Laravel-Modules. It supports module management in Laravel.
2. Reference URL: https://nicolaswidart.com/blog/writing-modular-applications-with-laravel-modules . Why use this package, write modular applications using laravel-modules. When it comes to writing more complex and larger applications, it is found that Laravel’s default structure is cumbersome and not ideal.
3. To install via Composer, run the following command. as shown in Figure 1
PS E:\wwwroot\laravel-modules-demo> composer require nwidart/laravel-modules Using version ^8.2 for nwidart/laravel-modules ./composer.json has been updated Running Composer Update Nwidart/Laravel-Modules Loading composer repositories with package information Updating dependencies lock file operations: 1 install, 0 updates, 0 removals - Locking Nwidart/Laravel-Modules (8.2.0) Writing lock file Installing dependencies from lock file (inclusion require-dev) Package operations: 1 install, 0 updates, 0 removes - downloading nwidart/laravel-modules (8.2.0) - Installing NWidArt/Laravel-Modules (8.2.0): Extracting Archive Package SwiftMailer/SwiftMailer is bandoned, you should avoid using it. Use symfony/mailer instead. GENERATING OPTIMIZED AUTOLOAD FILES > Illuminate\Foundation\ComposerScripts::PostAutoLoadDump > @php artisan package:discover --ansi Discovered Package: Facade/Ignition Discovered Package: Fruitcake/Laravel-Cors Discovered Package: Laravel/Sail Discovered Package: Laravel/Sanctum Discovered Package: Laravel/Tinker Discovered Package: Nesbot/Carbon Discovered Package: NUNOMADURO/COLLISION Discovered Package: Nwidart/Laravel-Modules Package manifested successfully. 78 packages you are using are looking for funding. Use the `Composer Fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force No Publishable Resources for Tag[laravel-assets]. publishing complete.
4. Run the following command to publish the configuration file of the package
PS E:\wwwroot\laravel-modules-demo> php artisan vendor:publish --provider="nwidart\modules\laravelModulesServiceProvider" Copied file[\vendor\nwidart\laravel-modules\config\config.php]to[\config\modules.php] publishing complete.
5. By default, the module class will not load automatically. You can automatically load modules using PSR-4. Edit composer.json and run: composer dump-autoload. as shown in Figure 2
"autoload": {
"PSR-4": {
"app\": "app/",
"modules\": "modules/",
"database\\factories\": "database/factories/",
"database\\seeders\": "database/seeders/"
}
},
PS e:\wwwroot\laravel-modules-demo> composer dump-autoload GENERATING OPTIMIZED AUTOLOAD FILES > Illuminate\Foundation\ComposerScripts::PostAutoLoadDump > @php artisan package:discover --ansi Discovered Package: Facade/Ignition Discovered Package: Fruitcake/Laravel-Cors Discovered Package: Laravel/Sail Discovered Package: Laravel/Sanctum Discovered Package: Laravel/Tinker Discovered Package: Nesbot/Carbon Discovered Package: NUNOMADURO/COLLISION Discovered Package: Nwidart/Laravel-Modules Package manifested successfully. GENERATED OPTIMIZED AUTOLOAD FILES CONTAINING 5106 CLASSES PS E:\wwwroot\laravel-modules-demo>
6. Reference document: https://nwidart.com/laravel-modules/v6/introduction , quickly open an example, use php artisan module:make blog Generate your first module. The following structure will be generated. Since the version of the document is for V6, and the latest version of the program is now V8, the directory structure is different. as shown in Figure 3
PS E:\wwwroot\laravel-modules-demo> php artisan module:make blog created : e:\wwwroot\laravel-modules-demo\modules/blog/module.json created : e:\wwwroot\laravel-modules-demo\modules/blog/routes/web.php created : e:\wwwroot\laravel-modules-demo\modules/blog/routes/api.php created : e:\wwwroot\laravel-modules-demo\modules/blog/resources/views/index.blade.php created : e:\wwwroot\laravel-modules-demo\modules/blog/resources/views/layouts/master.blade.php created : e:\wwwroot\laravel-modules-demo\modules/blog/config/config.php created : e:\wwwroot\laravel-modules-demo\modules/blog/composer.json created : e:\wwwroot\laravel-modules-demo\modules/blog/resources/assets/js/app.js created : e:\wwwroot\laravel-modules-demo\modules/blog/resources/assets/sass/app.scss created : e:\wwwroot\laravel-modules-demo\modules/blog/webpack.mix.js created : e:\wwwroot\laravel-modules-demo\modules/blog/package.json created : e:/wwwroot/laravel-modules-demo/modules/blog/database/seeders/blogDatabaseSeeder.php created : e:/wwwroot/laravel-modules-demo/modules/blog/providers/blogserviceprovider.php created : e:/wwwroot/laravel-modules-demo/modules/blog/providers/routeserviceprovider.php created : e:/wwwroot/laravel-modules-demo/modules/blog/http/controllers/blogcontroller.php module[Blog]Created successfully.
7. By default, when you create a new module, the command automatically adds some resources, such as controllers, torrents, service providers, etc. If you don’t want these, you can add the –plain flag to generate a normal module. The following structure will be generated. as shown in Figure 4
PS E:\wwwroot\laravel-modules-demo> php artisan module:make auth -p created : e:\wwwroot\laravel-modules-demo\modules/auth/module.json module[Auth]Created successfully.



