kanata-php / forklift
Livewire 包,用于移动模型。
Requires
- livewire/livewire: ^2.10
Requires (Dev)
- orchestra/testbench: ^7.0
README
Forklift
Laravel Livewire 组件
composer require kanata-php/forklift
使用 Eloquent 和 Livewire 以可预测和简单的方式移动资产。
依赖关系
- Laravel
- Livewire
- AlpineJS
安装
步骤 1
安装 composer 包
composer require kanata-php/forklift
步骤 2
发布 Service Provider 中注册的资产
php artisan vendor:publish --provider="Kanata\Forklift\ForkliftServiceProvider"
之后你可以在 resources/views/vendor/forklift
目录下找到资产。在当前版本中,初始资产仅适用于使用 Tailwind CSS 的项目。
步骤 3
为组件实现 Kanata\Forklift\Interfaces\AssetRepositoryInterface
,以便组件知道如何与你的项目模型交互。你需要为每种情况创建一个(例如,如果你需要移动嵌套目录,一个仓库,然后如果你需要通过目录移动文档,另一个仓库)。
方法
-
AssetRepositoryInterface::changeCurrentLocation
- 此方法返回当前位置的 "子位置"。当你有一个类似于文件系统导航的文件夹结构时,这相当于子文件夹。 -
AssetRepositoryInterface::moveAsset
- 此方法执行正在管理的资产在位置之间的移动。 -
AssetRepositoryInterface::findLocation
- 此方法查找当前位置对象。输出必须是一个包含以下字段的数组
- id (int) - 主键
- title (string) - 位置的标题
- parent (int) - 父位置(用于嵌套位置)
用法
基础
假设我们有一个文件系统结构。在这个结构中,你有文档和目录。此包添加了一个下拉菜单,允许快速在目录结构中移动资产。
对于此类,使用 Eloquent,我们会有的模型
- Directory - 保留目录结构,其中包含子目录和其中的文档。
示例迁移
Schema::create('directories', function (Blueprint $table) { $table->id(); $table->string('title', 40); $table->foreignId('parent')->nullable(); $table->timestamps(); });
- Document - 保留文档。
示例迁移
Schema::create('documents', function (Blueprint $table) { $table->id(); $table->string('title', 40); $table->longText('content'); $table->foreignId('directory_id')->nullable(); $table->timestamps(); });
以下组件是用于移动文档的 下拉菜单
@livewire('forklift-dropdown', [ 'currentLocationId' => $document->directory_id, 'locationType' => \App\Models\Directory::class, 'assetId' => $document->id, 'assetType' => \App\Models\Document::class, 'assetRepository' => App\Repositories\DocumentAssetRepository::class, 'parentField' => 'directory_id', ])
以下组件是用于移动目录的下拉菜单
@livewire('forklift-dropdown', [ 'currentLocationId' => $directory->parent, 'locationType' => \App\Models\Directory::class, 'assetId' => $directory->id, 'assetType' => \App\Models\Directory::class, 'assetRepository' => App\Repositories\DirecotryAssetRepository::class, 'parentField' => 'parent', ])
你可以在 这里 找到一个示例 Laravel 项目。
事件
Forklift 分发了 2 个事件,在 3 个不同的级别。这 2 个事件是
Kanata\Forklift\Events\AssetMoved
- 在资产仓库成功返回后触发。Kanata\Forklift\Events\AssetMoveFailed
- 在资产仓库失败返回后触发。
这些事件被分发的 3 个级别是
- Laravel 事件 (https://laravel.net.cn/docs/9.x/events#main-content)
- Livewire 事件 (https://laravel-livewire.com/docs/2.x/events)
- 通过 Livewire 的浏览器事件 (https://laravel-livewire.com/docs/2.x/events#browser)
待办事项
- 当后端发生错误时更改 UI。