Laravel 使用Caffeinated进行模块化开发

教程
packgist地址
添加链接描述

第三方镜像包是: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 模块名  模型名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值