learn2torials/modular-laravel

使用 Laravel modular 插件创建 Laravel 模块化应用

1.0.5 2021-07-17 17:49 UTC

This package is auto-updated.

Last update: 2024-09-09 19:46:21 UTC


README

Laravel

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

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

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

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

插件要求

  • PHP >= 8.0
  • Laravel >= 8.0

旧版 Laravel 插件

对于 Laravel 旧版本 <= 6,请使用此插件 Laravel Modular

如何安装此插件

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


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

# 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 目录中创建 config/modular.php 文件来开启此模块。

<?php

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

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

# without prefix
http://yourdomain.com/

# add prefix key in app/Modules/Comments/config.php file and then go to
http://yourdomain.com/comments

为你的模块启用翻译。在 config/modular.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',

    // module route prefix
    "prefix" => "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'
    ]
];

为你的模块添加前缀。设置你的模块的以下 config.php 文件

"prefix" => "admin",

现在,你的模块 URL 将是

http://yourdomain.com/admin/comments

如何运行模块迁移/种子

要为你的模块运行迁移或种子,请将种子添加到配置文件,并运行以下命令。


# run module migrations
php artisan migrate

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

参考

示例请参阅 https://learn2torials.com/a/laravel-module-management