mcpField
L
laravel-livewire

laravel-livewire

github.com/fusengine开发34152 次安装2 周前更新
代码仓库

关于 laravel-livewire

laravel-livewire 是面向 Laravel 13 与 Livewire 4 开发者的参考技能,涵盖响应式组件、表单、事件及测试等主题。它还整理 Volt、Folio、Precognition 和 Reverb 的用法与版本注意事项。

主要功能

  1. 01响应式 PHP 类组件与 Blade 视图
  2. 02wire:model 绑定、动作与生命周期钩子
  3. 03表单验证、事件及 Alpine.js 集成
  4. 04文件上传、组件嵌套与导航
  5. 05Volt、Folio、Precognition 与 Reverb 指南
  6. 06测试、安全建议及 Livewire 3 迁移说明

适用场景

  1. 01构建 Livewire 组件与交互式表单
  2. 02为搜索输入配置绑定与防抖
  3. 03实现组件事件和 Alpine.js 协作
  4. 04从 Livewire 3 迁移并检查安全实践

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/fusengine/agents --skill laravel-livewire

安装后在 Claude Code 中输入/laravel-livewire确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
alpine-integration.md
components.md
events.md
file-uploads.md
folio.md
forms-validation.md
lifecycle.md
loading-states.md
navigation.md
nesting.md
precognition.md
reverb.md
security.md
testing.md
volt.md
wire-directives.md
references/templates/
BasicComponent.php.md
ComponentTest.php.md
DataTableComponent.php.md
FileUploadComponent.php.md
FormComponent.php.md
NestedComponents.php.md
VoltComponent.blade.md

详细介绍

涵盖 Laravel 13 上的 Livewire 4:基于类的响应式组件与 Blade 视图、wire:model 双向绑定及其修饰符(.blur、.live、.debounce)、操作、组件生命周期钩子、表单/验证、事件(分发/监听)、Alpine.js 集成($wire、@entangle)、文件上传、组件嵌套、加载状态、SPA 导航、测试、安全性(身份验证/速率限制),以及 Volt(单文件组件)和 Folio(基于文件的路由)子功能,包括 Precognition 实时验证和 Reverb WebSocket 集成。

Laravel Livewire

Agent 工作流(强制)

在进行任何实现之前,并行启动 3 个 Agent,每个使用一个带有 name 的 Agent 调用:

  1. fuse-ai-pilot:explore-codebase - 检查现有 Livewire 组件
  2. fuse-ai-pilot:research-expert - 通过 Context7 验证 Livewire 4 模式
  3. mcp__context7__query-docs - 检查特定 Livewire 功能

实现完成后,运行 fuse-ai-pilot:sniper 进行验证。


概述

功能说明
组件带有 Blade 视图的响应式 PHP 类
wire:model双向数据绑定
操作从前端调用 PHP 方法
事件组件通信
Volt单文件组件
Folio基于文件的路由

关键规则

  1. 始终在循环中使用 wire:key
  2. 实时验证使用 wire:model.live.blur(Livewire 4.1+:单独使用 .blur 只会同步客户端状态),不要到处使用 .live
  3. 为搜索输入添加防抖,使用 .debounce.300ms
  4. 敏感 ID 使用 #[Locked]
  5. 在具有破坏性的操作中调用 authorize()
  6. 内部逻辑使用 protected 方法

决策指南

组件类型
Component choice?
├── Complex logic → Class-based component
├── Simple page → Volt functional API
├── Medium complexity → Volt class-based
├── Quick embed → @volt inline
└── File-based route → Folio + Volt
数据绑定
Binding type?
├── Form fields (live validation) → wire:model.live.blur
├── Search input → wire:model.live.debounce.300ms
├── Checkbox/toggle → wire:model.live
├── Select → wire:model
└── No sync → Local Alpine x-data

参考指南

核心概念(原因与架构)
主题参考资料适用场景
组件components.md创建组件
Wire 指令wire-directives.md数据绑定、事件
生命周期lifecycle.md钩子、mount、hydrate
表单forms-validation.md验证、表单对象
事件events.md分发、监听
Alpinealpine-integration.md$wire、@entangle
文件上传file-uploads.md上传处理
嵌套nesting.md父子组件
加载loading-states.mdwire:loading、延迟加载
导航navigation.mdSPA 模式
测试testing.md组件测试
安全性security.md身份验证、速率限制
Voltvolt.md单文件组件
高级功能
主题参考资料适用场景
Foliofolio.md基于文件的路由
Precognitionprecognition.md实时验证
Reverbreverb.mdWebSocket
模板(完整代码)
模板适用场景
BasicComponent.php.md标准组件
FormComponent.php.md带验证的表单
VoltComponent.blade.mdVolt 模式
DataTableComponent.php.md带搜索/排序的表格
FileUploadComponent.php.md文件上传
NestedComponents.php.md父子组件
ComponentTest.php.md测试模式

快速参考

基础组件
class Counter extends Component
{
    public int $count = 0;

    public function increment(): void
    {
        $this->count++;
    }

    public function render()
    {
        return view('livewire.counter');
    }
}
Volt 函数式组件
<?php
use function Livewire\Volt\{state};

state(['count' => 0]);

$increment = fn() => $this->count++;
?>

<button wire:click="increment">{{ $count }}</button>
Wire 指令
<input wire:model.live.blur="email">
<input wire:model.live.debounce.300ms="search">
<button wire:click="save" wire:loading.attr="disabled">Save</button>

最佳实践

应该
  • 在 @foreach 循环中使用 wire:key
  • 对搜索/筛选输入启用防抖
  • 使用 Form Objects 编写可复用逻辑
  • 使用 Livewire::test() 进行测试
  • ID 使用 #[Locked],派生数据使用 #[Computed]
不应该
  • 在每个字段上使用 wire:model.live
  • 在 render() 方法中查询
  • 忘记在操作中进行授权
  • 在循环中省略 wire:key
  • 在公共属性中存储敏感数据

Laravel 13 说明

Laravel 13 上的 Livewire 4

Livewire 4(当前版本为 4.4)是当前主版本,L13 Livewire 入门套件使用的正是该版本。要点如下:

  • Livewire 4 的要求:PHP 8.1+ / Laravel 10+(Laravel 13 无论如何都要求 PHP 8.3)
  • 仍支持 #[Locked]、#[Computed]、#[On]
  • 原生单文件/多文件组件(默认 make_command.type = sfc);Volt 仍为 1.x(当前版本为 1.11),Folio 仍为 1.x(当前版本为 1.2)
  • 页面:现在推荐使用 Route::livewire('/dashboard', Dashboard::class)(单文件/多文件组件必须使用此方法)
  • 哈希端点:使用 /livewire-{hash}/update(由 APP_KEY 派生),而非 /livewire/update——请更新针对 /livewire/ 的防火墙/CDN/中间件规则
  • wire:model.live:并行请求;wire:poll 不会阻塞
从 Livewire 3 迁移
  • 运行 composer require livewire/livewire:^4.0,然后运行 php artisan optimize:clear
  • 重命名的配置项:layout → component_layout(layouts::app),lazy_placeholder → component_placeholder;默认 smart_wire_keys = true
  • 4.1+:.blur / .change 也会控制客户端同步 → 如需旧行为(失焦时发送网络请求),请编写 wire:model.live.blur
  • wire:model 会忽略来自子组件的事件(如有需要,添加 .deep);<livewire:x /> 标签必须闭合
  • wire:transition 使用 View Transitions API(已移除修饰符);wire:scroll → wire:navigate:scroll
  • $this->stream():to: 重命名为 el:;$wire.$js('name', fn) 已弃用 → $wire.$js.name = fn
  • wire:poll.5s、$this->dispatch('event')、Form Objects(#[\Livewire\Attributes\Validate])→ 保持不变

评论

支持 Markdown