Skip to content

Action Overview

Actions are abstractions in Lartrix that describe user interaction behaviors, allowing you to define frontend logic with PHP code.

What is an Action?

An Action is a configuration object describing "what should happen when a user does something":

php
// When user clicks button
Button::make('Save')
    ->on('click', [
        // Set loading state
        SetAction::make('saving', true),
        // Send save request
        FetchAction::make('/api/posts')
            ->post()
            ->body(['title' => '{{ form.title }}'])
            ->then([
                SetAction::make('saving', false),
                CallAction::make('$message.success', ['Saved']),
                SetAction::make('visible', false),
            ]),
    ]);

Action Types

ActionDescription
SetActionSet reactive data
CallActionCall built-in or custom methods
FetchActionSend HTTP requests
IfActionConditional logic
ScriptActionExecute scripts
EmitActionTrigger custom events
CopyActionCopy to clipboard
WebSocketActionWebSocket operations

Method Chaining

Most Actions support method chaining:

php
FetchAction::make('/api/users/{id}')
    ->put()                           // HTTP method
    ->body(['status' => true])       // Request body
    ->headers(['X-Custom' => 'value']) // Headers
    ->then([...])                     // Success callback
    ->catch([...]);                   // Error callback

Detailed Documentation

基于 MIT 许可发布