laravel-livewire

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Laravel Livewire

Laravel Livewire

Agent Workflow (MANDATORY)

Agent 工作流(必填)

Before ANY implementation, use
TeamCreate
to spawn 3 agents:
  1. fuse-ai-pilot:explore-codebase - Check existing Livewire components
  2. fuse-ai-pilot:research-expert - Verify Livewire 3 patterns via Context7
  3. mcp__context7__query-docs - Check specific Livewire features
After implementation, run fuse-ai-pilot:sniper for validation.

任何实现工作开始前,使用
TeamCreate
生成3个Agent:
  1. fuse-ai-pilot:explore-codebase - 检查现有Livewire组件
  2. fuse-ai-pilot:research-expert - 通过Context7验证Livewire 3的开发模式
  3. mcp__context7__query-docs - 查阅Livewire的特定功能
实现完成后,运行fuse-ai-pilot:sniper进行验证。

Overview

概述

FeatureDescription
ComponentsReactive PHP classes with Blade views
wire:modelTwo-way data binding
ActionsCall PHP methods from frontend
EventsComponent communication
VoltSingle-file components
FolioFile-based routing

特性描述
Components搭配Blade视图的响应式PHP类
wire:model双向数据绑定
Actions从前端调用PHP方法
Events组件间通信
Volt单文件组件
Folio基于文件的路由

Critical Rules

关键规则

  1. Always use wire:key in loops
  2. Use wire:model.blur for validation, not .live everywhere
  3. Debounce search inputs with .debounce.300ms
  4. #[Locked] for sensitive IDs
  5. authorize() in destructive actions
  6. protected methods for internal logic

  1. 在循环中必须使用wire:key
  2. 验证场景下使用wire:model.blur,不要到处用.live
  3. 为搜索输入添加防抖处理,使用.debounce.300ms
  4. 敏感ID使用**#[Locked]**注解
  5. 破坏性动作中调用authorize()
  6. 内部逻辑使用protected方法

Decision Guide

决策指南

Component Type

组件类型

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
选择组件类型?
├── 复杂逻辑 → 基于类的组件
├── 简单页面 → Volt函数式API
├── 中等复杂度 → Volt基于类的组件
├── 快速嵌入 → @volt内联方式
└── 基于文件的路由 → Folio + Volt

Data Binding

数据绑定

Binding type?
├── Form fields → wire:model.blur
├── Search input → wire:model.live.debounce.300ms
├── Checkbox/toggle → wire:model.live
├── Select → wire:model
└── No sync → Local Alpine x-data

选择绑定类型?
├── 表单字段 → wire:model.blur
├── 搜索输入 → wire:model.live.debounce.300ms
├── 复选框/切换按钮 → wire:model.live
├── 下拉选择框 → wire:model
└── 无需同步 → 本地Alpine x-data

Reference Guide

参考指南

Core Concepts (WHY & Architecture)

核心概念(设计思路与架构)

TopicReferenceWhen to Consult
Componentscomponents.mdCreating components
Wire Directiveswire-directives.mdData binding, events
Lifecyclelifecycle.mdHooks, mount, hydrate
Formsforms-validation.mdValidation, form objects
Eventsevents.mdDispatch, listen
Alpinealpine-integration.md$wire, @entangle
File Uploadsfile-uploads.mdUpload handling
Nestingnesting.mdParent-child
Loadingloading-states.mdwire:loading, lazy
Navigationnavigation.mdSPA mode
Testingtesting.mdComponent tests
Securitysecurity.mdAuth, rate limit
Voltvolt.mdSingle-file components
主题参考文档查阅场景
Componentscomponents.md创建组件时
Wire Directiveswire-directives.md数据绑定、事件处理时
Lifecyclelifecycle.md钩子函数、mount、hydrate阶段
Formsforms-validation.md验证、表单对象时
Eventsevents.md事件分发、监听时
Alpinealpine-integration.md$wire、@entangle使用时
File Uploadsfile-uploads.md文件上传处理时
Nestingnesting.md父子组件开发时
Loadingloading-states.mdwire:loading、懒加载使用时
Navigationnavigation.mdSPA模式开发时
Testingtesting.md组件测试时
Securitysecurity.md认证、速率限制时
Voltvolt.md单文件组件开发时

Advanced Features

高级特性

TopicReferenceWhen to Consult
Foliofolio.mdFile-based routing
Precognitionprecognition.mdLive validation
Reverbreverb.mdWebSockets
主题参考文档查阅场景
Foliofolio.md基于文件的路由开发时
Precognitionprecognition.md实时验证时
Reverbreverb.mdWebSockets使用时

Templates (Complete Code)

模板(完整代码)

TemplateWhen to Use
BasicComponent.php.mdStandard component
FormComponent.php.mdForm with validation
VoltComponent.blade.mdVolt patterns
DataTableComponent.php.mdTable with search/sort
FileUploadComponent.php.mdFile uploads
NestedComponents.php.mdParent-child
ComponentTest.php.mdTesting patterns

模板使用场景
BasicComponent.php.md标准组件
FormComponent.php.md带验证的表单
VoltComponent.blade.mdVolt组件模式
DataTableComponent.php.md带搜索/排序的表格
FileUploadComponent.php.md文件上传
NestedComponents.php.md父子组件
ComponentTest.php.md测试模式

Quick Reference

快速参考

Basic Component

基础组件

php
class Counter extends Component
{
    public int $count = 0;

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

    public function render()
    {
        return view('livewire.counter');
    }
}
php
class Counter extends Component
{
    public int $count = 0;

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

    public function render()
    {
        return view('livewire.counter');
    }
}

Volt Functional

Volt 函数式组件

php
<?php
use function Livewire\Volt\{state};

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

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

<button wire:click="increment">{{ $count }}</button>
php
<?php
use function Livewire\Volt\{state};

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

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

<button wire:click="increment">{{ $count }}</button>

Wire Directives

Wire 指令

blade
<input wire:model.blur="email">
<input wire:model.live.debounce.300ms="search">
<button wire:click="save" wire:loading.attr="disabled">Save</button>

blade
<input wire:model.blur="email">
<input wire:model.live.debounce.300ms="search">
<button wire:click="save" wire:loading.attr="disabled">Save</button>

Best Practices

最佳实践

DO

建议做法

  • Use wire:key in @foreach loops
  • Debounce search/filter inputs
  • Use Form Objects for reusable logic
  • Test with Livewire::test()
  • #[Locked] for IDs, #[Computed] for derived data
  • 在@foreach循环中使用wire:key
  • 对搜索/筛选输入添加防抖处理
  • 使用表单对象实现可复用逻辑
  • 使用Livewire::test()进行测试
  • 对ID使用#[Locked]注解,对派生数据使用#[Computed]注解

DON'T

不建议做法

  • wire:model.live on every field
  • Query in render() method
  • Forget authorization in actions
  • Skip wire:key in loops
  • Store sensitive data in public properties
  • 对所有字段都使用wire:model.live
  • 在render()方法中执行查询
  • 动作中忘记添加授权验证
  • 循环中省略wire:key
  • 在公共属性中存储敏感数据