salodev/modularize

laravel 的模块功能

v0.0.3 2023-02-27 12:02 UTC

This package is auto-updated.

Last update: 2024-09-27 15:20:10 UTC


README

这个包为您的 Laravel 项目提供了模块化功能。

因此,您现在可以更好地提高代码组织,提高生产效率,并减少代码分析时间。

您有一套 artisan 命令,用于简化大多数常见任务,例如创建模块及其组件。

文件骨架

从现在开始,您将使用以下结构进行工作

    app\
        Modules\
            ModuleName\
                Commands\
                Mails\
                Migrations\
                Models\
                Requests\
                Resources\
                Views\
                ModuleNameController.php
                ModuleNameModule.php
            AppModule.php

安装

composer require salodev/modularize
composer require salodev/modularize-generator --dev

安装后,AppModule.php 文件将被放置在 app\Modules 文件夹中,它是主要模块注册点

app\
    Modules\
        AppModules.php

其内容如下所示

<?php

namespace App\Modules;

use Salodev\Modularize\Module;

class AppModule extends Module {

}

代码生成器

为了便于工作,该包提供了一套命令,用于实现最常见的任务,如正常的 Laravel 开发。这些命令位于 modularize: 命名空间下

要查看它们,请运行 list artisan 命令

php artisan list modularize

输出将类似于以下内容

  modularize:add:route         Add a module route
  modularize:add:schedule      Add a sheduled command
  modularize:list:migrations   List all migrations
  modularize:list:modules      List all modules
  modularize:make:command      Make a module command
  modularize:make:config       Make a module config file
  modularize:make:controller   Make a module controller
  modularize:make:crud-module  Make a CRUD module
  modularize:make:mail         Make a module mail
  modularize:make:migration    Make a module migration
  modularize:make:model        Make a module model
  modularize:make:module       Make a module
  modularize:make:request      Make a module controller
  modularize:make:resource     Make a module resource

这些命令将生成指定模块中的常见文件。

要了解更多关于任何命令选项的信息,请请求它帮助,如下所示

php artisan modularize:make:module --help

创建模块

只需键入

php artisan modularize:make:module

命令是交互式的。首先,询问父模块,从先前生成的模块列表中选择。第一个是由安装创建的 app。一旦选择了父模块,就询问一个名称,并创建具有新类定义的模块文件。

所有模块都依赖于根模块或任何子模块。组成级别是无限的。

每个模块都可以定义以下组件

  • Http 路由、控制器和请求
  • 资源
  • 模型
  • 控制台命令
  • 计划任务
  • 数据库迁移和种子
  • 邮件和模板
  • 配置文件
  • 它自己的模块

其中一些组件必须在模块内部注册或初始化。其他组件将自动注册。

使用 provide() 方法在 register() 方法中注册子模块

    public function register() {
        $this->provide(ChildModule::class);
    }

当您通过 artisan 命令创建子模块时,它会检查父模块,如果尚未创建,则会生成 register() 方法,或者仅添加 provide() 调用

创建模块路由

每个模块定义自己的路由。您可以手动添加新路由或使用 artisan 命令

artisan 方法

使用以下 artisan 命令快速创建路由

php artisan modularize:add:route

程序将询问模块、动词、名称和资源名称。在其结束时,您的代码将被正确更新。

为了避免使用交互式模式,查看参数列表

php artisan modularize:add:route --help

手动方法

转到您想要添加路由的模块

为 API 路由添加或编辑 bootApiRoutes 方法,或为 Web 路由添加 bootWebRoutes

以下示例显示了如何为 API 添加新路由

class UsersModule {
    public function bootApiRoutes() {
        $this->router()->get('', [UsersController::class, 'index']);

        // For api authorized requests
        $this->router()->middleware('auth:api')->group(function() {
            $this->router()->get('/me', [UsersController::class, 'me']);
        });
    }
}

更改模块路由前缀

默认情况下,生成的路由将附加模块名称。但您可以为其他名称定义 routePrefix 模块属性

    $this->routePrefix = 'my-route-prefix'

    // another case
    $this->routePrefix = '{account}' // for GET user/{account}

迁移

迁移模块存储在模块文件夹内的 Migrations 文件夹中

以下命令创建模块迁移

php artisan modularize:make:migration

因为没有提供参数,所以会交互式地询问。

因此,您可以列出创建的迁移并检查状态

php artisan migrate:status

您还可以检查模块的迁移,以了解它们是在哪里创建的

php artisan modularize:list:migrations

输出将类似于以下内容

+------+-----------------------------------------+-----------------------------------+-------+
| Ran? | Migration                               | Directory                         | Batch |
+------+-----------------------------------------+-----------------------------------+-------+
| No   | 2023_01_21_122840_create_article_table  | /app/Modules/Articles/Migrations  |       |
| No   | 2023_01_21_165751_create_customer_table | /app/Modules/Customers/Migrations |       |
| No   | 2023_01_21_165843_create_sale_table     | /app/Modules/Sales/Migrations     |       |
+------+-----------------------------------------+-----------------------------------+-------+

注意,您有 目录 列表来查看每个迁移放置的位置

要运行所有迁移,只需像以前一样调用即可

php artisan migrate

配置

模块定义了自己的配置,并且它们存储在模块根目录下的 config.php 文件中

文件

app\
    Modules\
        Service\
            ServiceModule.php
            config.php        <-- here
        AppModule.php

以下是 app\Modules\Service\ServiceModule\config.php 文件的示例内容

return [
    'api-key' => env('APP_SERVICE_API_KEY')
];

要获取其配置,只需调用所需模块的 ::config() 方法,如下所示

$apiKey = \App\Modules\Service\ServiceModule::config('api-key');

注意,也可以通过 Laravel 的 config() 辅助函数访问

config('app.service.api-key');

可以使用命令生成配置文件

php artisan modularize:make:config

所有配置键将以 app 命名空间为根,以避免与包配置冲突

所有配置键将嵌套在父模块键中

模块键是模块文件夹的短横线命名

使用建议

为了保持项目模块化,将每个模块视为一个包或服务本身。因此,尽量将所有功能保留在模块或子模块中。

想象一个扩展场景,你应该如何拆分应用程序为微服务或依赖包。一个良好的模块化项目不应该是一团糟,因为只需要移动文件,而不需要修改它们。

对于文件环境变量,尽量在变量名中包含模块链路径以及配置名称,如下所示

文件:app/Articles/config.php,配置变量名称:expiration-time:APP_ARTICLES_EXPIRATION_TIME

生产环境考虑

为了避免增加生产环境部署包的大小,这个库被分为两部分

  • salodev/modularize 依赖包,提供 Laravel 的模块化支持
  • salodev/modularize-generator 依赖包,提供控制台命令生成。考虑将其作为 dev 依赖安装。

因此,当您部署到无服务器环境,如 AWS Lambda 时,使用 composer 的 --no-dev 选项进行捆绑。这样,只有 salodev/modularize 会被捆绑。

许可

此软件包随 Unlicense 提供使用