- Laravel多模块组件 Laravel-Modules
- https://github.com/nWidart/laravel-modules
- https://www.jianshu.com/p/09c2375a39ad
开发应用时经常会需要将应用划分为不同的模块,Laravel-Modules扩展使用模块帮助管理大型应用。模块和扩展包类似,都包含视图、控制器、模型等子组件,但使用模块区分功能单元时更加清晰。
Laravel-Modules扩展功能
- 自动注册服务提供者和别名
- 自动创建模块相关的artisan命令
- 项目根目录下生成多模块目录Modules
安装配置
查看当前Coomposer版本
$ composer -V
Composer version 1.8.5 2019-04-09 17:46:47
更新Composer到最新版
$ composer self-update
Updating to version 2.0.8 (stable channel).
Downloading (100%)
Use composer self-update --rollback to return to version 1.8.5
安装扩展
$ composer require nwidart/laravel-modules
$ composer update
Laravel5.5新增包自动发现功能,使得Laravel更加容易对包进行安装和启用进行管理。当使用composer require
安装包时,Laravel会自动注册包的服务提供者并启用。
发布组件
$ php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
Copied File [\vendor\nwidart\laravel-modules\config\config.php] To [\config\modules.php]
Publishing complete.
发布成功后会在config
配置目录下生成modules.php
多模块配置文件
自动加载
在项目资源包配置文件composer.json
的自动加载项目中的PSR-4规范中添加命令空间与路径的映射。
$ vim composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "modules/"
}
},
将项目中修改后的自动加载写入vendor中自动加载的类映射文件中 vendor/composer/autoload_classmap.php
。
$ composer dump-autoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
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 4334 classes
创建模块
创建后台Admin模块
$ php artisan module:make Admin
Created : D:\laravel\projects\nirvana\Modules/Admin/module.json
Created : D:\laravel\projects\nirvana\Modules/Admin/Routes/web.php
Created : D:\laravel\projects\nirvana\Modules/Admin/Routes/api.php
Created : D:\laravel\projects\nirvana\Modules/Admin/Resources/views/index.blade.php
Created : D:\laravel\projects\nirvana\Modules/Admin/Resources/views/layouts/master.blade.php
Created : D:\laravel\projects\nirvana\Modules/Admin/Config/config.php
Created : D:\laravel\projects\nirvana\Modules/Admin/composer.json
Created : D:\laravel\projects\nirvana\Modules/Admin/Resources/assets/js/app.js
Created : D:\laravel\projects\nirvana\Modules/Admin/Resources/assets/sass/app.scss
Created : D:\laravel\projects\nirvana\Modules/Admin/webpack.mix.js
Created : D:\laravel\projects\nirvana\Modules/Admin/package.json
Created : D:/laravel/projects/nirvana/Modules/Admin/Database/Seeders/AdminDatabaseSeeder.php
Created : D:/laravel/projects/nirvana/Modules/Admin/Providers/AdminServiceProvider.php
Created : D:/laravel/projects/nirvana/Modules/Admin/Providers/RouteServiceProvider.php
Created : D:/laravel/projects/nirvana/Modules/Admin/Http/Controllers/AdminController.php
Module [Admin] created successfully.
模块创建成功后,会自动在项目根目录下的Modules文件夹下生成Admin后台模块。
运行测试
运行本地开发服务器测试
$ php artisan serve
访问后台路由 http://127.0.0.1:8000/admin
授权登录
在环境配置.env
中设置本地MySQL数据连接选项后,在项目根目录下执行数据迁移,创建Laravel自带的数据表。
$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (0.52 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (0.41 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (0.3 seconds)
创建后台登录控制器
$ php artisan module:make-controller LoginController Admin
Created : D:/laravel/projects/nirvana/Modules/Admin/Http/Controllers/LoginController.php