learn2torials/laravel-modular

Laravel 模块化应用程序生成插件

1.0.9 2021-07-17 17:56 UTC

This package is auto-updated.

Last update: 2024-09-18 01:16:19 UTC


README

Laravel Release Issues Licence

将现有的 Laravel 应用程序转换为模块化应用程序。Laravel 模块化插件允许您为 Laravel 编写模块化插件。

比如说,您正在构建一个博客应用程序。您的博客需要以下功能

  • 评论
  • 博客文章
  • 用户管理等。

您可以将这些功能转换为模块并将您的逻辑捆绑在一起,以便您可以轻松地将此模块用于其他项目。您可以轻松地打开/关闭您的模块。

插件要求

  • PHP >= 7.2
  • Laravel >= 6.0

较新的 Laravel 插件

对于 Laravel 8.0 及以上版本的旧版本,请使用此插件 Modular Laravel

如何安装此插件

在现有项目中运行以下命令。


# install this plugin
composer require "learn2torials/laravel-modular"

# create module using artisan
php artisan make:module comments

上述命令将在 App/Modules 下创建一个新目录,具有以下结构。

App
|- Modules
   |-- Comments
      |-- Controllers
      |-- Models
      |-- Views
      |-- Migrations
         |-- Seeder
      |-- Translations
         |-- en
             |-- general.php
         |-- fr
             |-- general.php
      |-- config.php
      |-- routes.php

接下来,一旦生成此目录结构,您可以通过在 config 目录中创建 console.php 文件来启用此模块。

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "prefix"   => null,
    "i18n"     => false,
    "https"    => false,
    "modules"  => [
        "comments" => true
    ]
];

就这样,您的模块现在已启用。您可以通过浏览来验证您的模块是否正常工作

http://yourdomain.com/comments

在所有模块之前添加前缀。在 config/console.php 文件中设置以下配置。

"prefix" => "admin",

现在,您的模块 URL 将是

http://yourdomain.com/admin/comments

为您的模块启用翻译。在 config/console.php 文件中设置以下配置。

"i18n" => true,

现在,您的模块 URL 将是

http://yourdomain.com/en/ca/comments      -> for english translation
http://yourdomain.com/fr/ca/comments      -> for french translation

当前缀启用时

http://yourdomain.com/en/ca/admin/comments -> if prefix is admin
http://yourdomain.com/fr/ca/admin/comments -> if prefix is admin

如何使用翻译。检查您的模块中的视图文件以获取使用方法

{{ __('module::file_name.translation_key') }}

模块配置

一旦模块启用,您可以使用以下语法访问与模块相关的配置。

例如:如果您安装了一个用户模块,用户模块的配置文件位于 Modules/User/config.php

<?php

/*
|--------------------------------------------------------------------------
| User Module Configurations
|--------------------------------------------------------------------------
|
| Here you can add configurations that relates to your [User] module
| Only make sure not to add any other configs that do not relate to this
| User Module ...
|
*/
return [

    // module name
    'name' => 'User',

    // register middleware
    'middleware' => [
        'user' => \App\Modules\User\Middleware\UserAuthenticated::class,
    ],

    // register service providers
    'providers' => [
        \App\Modules\User\Provider\UserProvider::class
    ],

    // register route middleware
    'route_middleware' => ['user'],

    // database seeder
    'seeder' => [
        __DIR__. '/Migrations/Seeder/UsersTableSeeder.php'
    ]
];

如何运行模块迁移/生成器

要为您的模块运行迁移或生成器,请向配置文件中添加生成器,并运行以下命令。


# run module migrations
php artisan migrate

# run module seeders
php artisan db:seed --class="L2T\Database\Seeder"

参考

示例显示在 https://learn2torials.com/a/laravel-module-management