第三方镜像包是:Caffeinated/laravel-modules,laravel 的模块管理器。用起来很方便,git 地址:
安装:
使用 composer 快捷安装:
composer require caffeinated/modules
当然这一步是要建立在你本地已经安装了 composer 的情况下。不会安装 composer 可以查看官方说明,地址:https://getcomposer.org/download/
添加服务提供者:
接下来在 config /app.php 中添加以下服务提供者。
'providers' => [
Caffeinated\Modules\ModulesServiceProvider::class,
],
添加别名到同一文件的别名数组中。
'aliases' => [
'Module' => Caffeinated\Modules\Facades\Module::class,
],
以上两步是进行服务注册,必须要进行添加,不过在 laravel5.5 版本之后可以不添加也能运行,这得益于 laravel5.5 提供的包自动发现机制。
接下来发布软件包的配置,通过运行以下方式:
php artisan vendor:publish --provider="Caffeinated\Modules\ModulesServiceProvider" --tag="config"
执行完上面这条命令之后,在 config 文件夹下会生成一个 modules.php 文件,这个是模块开发的配置文件,你可以在这里面进行配置。
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Location
|--------------------------------------------------------------------------
|
| This option controls the default module location that gets used while
| using this package. This location is used when another is not explicitly
| specified when exucuting a given module function or command.
|
*/
'default_location' => 'app',
/*
|--------------------------------------------------------------------------
| Locations
|--------------------------------------------------------------------------
|
| Here you may define all of the module locations for your application as
| well as their drivers and other configuration options. This gives you
| the flexibility to structure modules as you see fit in each location.
|
*/
'locations' => [
'app' => [
'driver' => 'local',
'path' => app_path('Modules'),
'namespace' => 'App\\Modules\\',
'enabled' => true,
'provider' => 'ModuleServiceProvider',
'manifest' => 'module.json',
'mapping' => [
// Here you may configure the class mapping, effectively
// customizing your generated default module structure.
'Config' => 'Config',
'Database/Factories' => 'Database/Factories',
'Database/Migrations' => 'Database/Migrations',
'Database/Seeds' => 'Database/Seeds',
'Http/Controllers' => 'Http/Controllers',
'Http/Middleware' => 'Http/Middleware',
'Providers' => 'Providers',
'Resources/Lang' => 'Resources/Lang',
'Resources/Views' => 'Resources/Views',
'Routes' => 'Routes'
],
],
],
/*
|--------------------------------------------------------------------------
| Default Driver
|--------------------------------------------------------------------------
|
| Here you may specify the default module storage driver that should be
| used by the package. A "local" driver is available out of the box that
| uses the local filesystem to store and maintain module manifests.
|
*/
'default_driver' => 'local',
/*
|--------------------------------------------------------------------------
| Drivers
|--------------------------------------------------------------------------
|
| Here you may configure as many module drivers as you wish. Use the
| local driver class as a basis for creating your own. The possibilities
| are endless!
|
*/
'drivers' => [
'local' => 'Caffeinated\Modules\Repositories\LocalRepository',
],
];
使用
可以使用make:moduleArtisan命令创建模块:
php artisan make:module Listing
php artisan module:optimize
composer dump-autoload
如果需要一次生成多个模块,可以使用以下命令:
php artisan make:module:provider Listing PublisherServiceProvider
php artisan module:optimize
composer dump-autoload
Service Providers
生成modle
php artisan make:module:model 模块名 模型名