viandwi24 / laravel-module-system
适用于 Laravel 的模块系统
Requires
- php: >=7
- adbario/php-dot-notation: ^2.2
- illuminate/support: ^7
This package is auto-updated.
Last update: 2024-09-05 21:49:04 UTC
README
将模块系统添加到您的 Laravel 应用程序,使用 # 模块化应用架构。这个概念很简单,提取默认的服务提供程序 laravel,然后重新编程使其再次加载并像模块/插件一样进行控制。
规范
- 核心版本:1.0.5
- Laravel 支持:7.x
安装
使用 composer 将其添加到您的项目中
composer require viandwi24/laravel-module-system
将服务提供程序添加到您的 config/app.php
Viandwi24\ModuleSystem\ServiceProvider::class,
将服务提供程序添加到您的 config/app.php
'Module' => Viandwi24\ModuleSystem\Facades\Module::class
并且,您可以发布配置
php artisan vendor:publish --provider="Viandwi24\ModuleSystem\ServiceProvider"
用法
默认页面
我们有一个默认页面来控制和管理工作流,默认页面可以通过 https://:8000/module
访问。您可以通过更改配置来更改默认页面 URL 或禁用此页面。
在 config/module.php
中配置
'default_page' => true,
'default_page_prefix' => 'module',
'default_page_middleware' => [],
运行 Laravel
php artisan serve
您可以在浏览器中访问此 URL
https://:8000/module
例如,您可以从 此链接 下载示例模块,然后转到 https://:8000/module
并点击 "安装 .zip",上传 examplemodule.zip 点击,在模块列表中激活
最后,转到 https://:8000/tes
并在浏览器中查看
是的,路由 https://:8000/tes
是从 ExampleModule
生成的动态路由,如果您禁用此插件,那么当您再次访问此路由时,您将看到 404 页面。
模块外观
模块类提供了几乎全部的模块引用,您可以使用它来
Viandwi24\ModuleSystem\Facades\Module
获取模块列表
Module::get();
启用模块
Module::enable($module_name);
禁用模块
Module::disable($module_name);
创建模块
模块是受服务提供程序启发的,现有的模块利用了 Laravel 内置的简单易用的服务提供程序。
php artisan make:module ExampleModule
模块结构
模块具有简单的结构,默认模块文件夹位于 app/Modules
中。例如,我们已使用以下命令在 app/Modules/ExampleModule
中创建了一个新模块 ExampleModule
:
php artisan make:module ExampleModule
文件内容
- module.js
- ExampleModuleServiceProvider
modules.js
这是一个模块的主要配置文件,包含以下信息:
{
"name" : "ExampleModule",
"description" : "Description your module",
"version" : "1.0",
"author" : "viandwi24",
"email" : "fiandwi0424@gmail.com",
"web" : "viandwi24.github.io",
"namespace" : "ExampleModule",
"service" : "ExampleModuleServiceProvider"
}
- namespace:模块的主要命名空间,如果在本例中,该命名空间将解析为
App\Modules\ExampleModule
- service:模块的主要类,将被执行,遵循 Laravel 内置服务提供程序修改后的方式。
服务类 - ExampleModuleServiceProvider.php
此文件执行的方式类似于 Laravel 中的服务提供程序,以下是其主要结构:
<?php
namespace App\Modules\ExampleModule;
use Viandwi24\ModuleSystem\Base\Service;
use Viandwi24\ModuleSystem\Interfaces\ModuleInterface;
class ExampleModuleServiceProvider extends Service implements ModuleInterface
{
public function register()
{
//
}
public function boot()
{
//
}
public function check()
{
return [
'state' => 'ready'
];
}
}
- register:当 Laravel 启动时将执行的函数
- boot:当所有 Laravel 启动完成时将执行的函数
- check:在所有其他模块注册后运行的函数。此函数用于返回一个状态数组。
check() 方法
此函数用于返回一个状态数组的值。
Ready
Ready 状态将使模块开始启动
['state' => 'ready']
Not Ready
Not Ready 状态将使模块保持启动状态,但会显示一个警告,说明模块尚未准备好并需要设置。将显示设置选项,设置值包含用于进行模块配置的链接,以便将来可以将其设置为 Ready。
[
'state' => 'not_ready',
'setup' => route('my_module_route.setup')
]
Error
Error 状态将使模块无法加载,并在模块管理中显示错误警告。
[
'state' => 'error',
'error' => 'Tidak support dengan versi laravel anda!'
]