Skip to content

Your First Linker Component ​

Goal: a two-step ticket form. Step one takes a subject; the server validates it, remembers it, and moves to step two.

Step 0: let the generator start you off ​

bash
php artisan make:linker Ticket

This creates app/Http/LinkerComponents/TicketLinker.php and writes 'ticket' => TicketLinker::class into LinkerServiceProvider. Add --dry-run to see what it would write first.

The three steps below are what the generator does for you. Knowing what they look like is what lets you change them.

Step 1: write the component ​

php
namespace App\Http\LinkerComponents;

use App\Attributes\Linker\Computable;
use App\Attributes\Linker\RequiresAuth;
use App\Attributes\Linker\Validate;
use App\Support\Linker\LinkerResponse;

#[RequiresAuth]
class TicketLinker extends LinkerComponent
{
    public static string $alias = 'ticket';

    public function mount(array $params = []): void
    {
        $this->set('step', 1);          // server-side state: the browser can't see or change it
    }

    #[Computable]
    #[Validate(['subject' => 'required|string|max:120'])]
    public function saveStep1(array $args): LinkerResponse
    {
        $this->set('subject', $args['subject']);
        $this->set('step', 2);

        return $this->ok(['step' => 2])
            ->call('dom.scrollTo', ['target' => '#step-2']);
    }
}

Key points:

  • #[Computable]: methods without it don't exist as far as the browser is concerned.
  • #[Validate]: Laravel validation rules. Parameters not listed are never received.
  • $this->set() / $this->get(): server-side state (Stateful mode).
  • $this->ok($data): data is written into client state; templates read it with data-state-*.
  • ->call(): appends a browser action.

Step 2: register the alias ​

app/Providers/LinkerServiceProvider.php:

php
LinkerRegistry::registerMany([
    'ticket' => TicketLinker::class,
    // make:linker          ← the generator inserts new lines above this
]);

The browser sends the alias, not the class name.

There is deliberately no auto-discovery. This table is the complete answer to "which components can the browser call?". If a directory were scanned instead, dropping a file into it would open a door, and that would never show up prominently in a diff.

Step 3: the template ​

blade
<div @linker('ticket') @renderObject('ticketWizard', ['state:read'])>
    <form>
        <input name="subject">
        <button type="button" data-linker-call="saveStep1" data-linker-args="form">Next</button>
    </form>

    <div id="step-2" data-state-is="step=2">
        Step two…
    </div>
</div>
  • @linker('ticket') is the mount point. In Stateful mode it outputs the instance uuid and rev.
  • data-linker-args="form" collects every field of the nearest <form>.
  • If validation fails, the error message is painted next to the subject field automatically; you write nothing.
  • data-state-is reads state, so @renderObject must grant state:read.

Done. No new route, no controller, no resource, no front-end code.

Lifecycle ​

  • Calls on the same instance are locked and run one at a time. A request carrying an old rev gets conflict.
  • After an idle period or the hard lifetime cap, calls return expired and the user is asked to reload.
  • Reloading creates a brand-new instance; state that never reached the database is gone. To resume a form, save a draft row in the database.

Caching results ​

Read methods can be cached. The cache key always includes the user id and the arguments:

php
#[Computable(cache: true, seconds: 60)]
public function stats(array $args): LinkerResponse { … }

The cache is read only after the permission checks.

Next: Mental Model, then Linker Template Attributes.