Laravel8 多模块
1. 根目录(不是public目录)安装laravel model 扩展包
composer require nwidart/laravel-modules
2. 生成config配置命令(5.5版本 以下不用注册 高版本的需要)
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
3. 修改项目根目录下composer.json文件
"autoload": {undefined "psr-4": {undefined "App\\": "app/", "Modules\\": "Modules/" //再这个"autoload": 加上 "Modules\\": "Modules/" } }
4. 新增模块命令
php artisan module:make module-name
eg:
php artisan module:make User
php artisan module:make Api
php artisan module:make Admin
5.
执行成功之后 执行一下下面的命令
composer dump-autoload
到此算是多模块化执行成功
6.增加路由
首先在\routes文件夹下新增admin.php路由文件,然后他相应路由粘贴过来,
修改\app\Providers\RouteServiceProvider.php文件,加入如下代码
Route::middleware('admin') ->namespace($this->namespace) ->group(base_path('routes/admin.php'));
修改app/Http目录下Kernel.php文件
$middlewareGroups 'admin' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ],