在 Laravel 8 中的模块化示例实现
1、在 GitHub 中搜索:laravel module,选择第一个结果:nWidart / laravel-modules。其支持 Laravel 中的模块管理。
2、参考网址:https://nicolaswidart.com/blog/writing-modular-applications-with-laravel-modules 。为什么要使用这个包,使用 laravel-modules 编写模块化应用程序。当涉及到编写更复杂、更大的应用程序时,发现 laravel 的默认结构很麻烦而且不理想。
3、要通过 Composer 安装,请运行以下命令。如图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 (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading nwidart/laravel-modules (8.2.0) - Installing nwidart/laravel-modules (8.2.0): Extracting archive Package swiftmailer/swiftmailer is abandoned, 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 manifest generated 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、运行以下命令发布包的配置文件
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、默认情况下,模块类不会自动加载。您可以使用 psr-4 自动加载模块。编辑 composer.json,然后运行:composer dump-autoload。如图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 manifest generated successfully. Generated optimized autoload files containing 5106 classes PS E:\wwwroot\laravel-modules-demo>
6、参考文档:https://nwidart.com/laravel-modules/v6/introduction ,快速开启一个示例,使用 php artisan module:make Blog 生成你的第一个模块。 将生成以下结构。由于文档的版本是针对 v6 的,而现在程序的最新版本是 v8,因此,目录结构有所出入。如图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、默认情况下,当您创建新模块时,该命令会自动添加一些资源,例如控制器、种子类、服务提供者等。如果你不想要这些,你可以添加 –plain 标志,生成一个普通的模块。将生成以下结构。如图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.
近期评论