Skip to content

Governing Actions ​

The server doesn't send JavaScript. It sends { fn: 'dom.scrollTo', args: {...} }, and the browser looks it up in a table. That table is the complete answer to "the most the server can make the browser do", so manage it the way you manage permissions.

Two tables, one-to-one ​

SideWhereContents
PHPLinkerServiceProvider::registerActions()Action name + argument spec
JSresources/js/linker/actions.jsAction name + implementation
php
LinkerAction::defineMany([
    'dom.scrollTo'    => ['target' => 'string'],
    'dom.class'       => ['target' => 'string', 'class' => 'string', 'on' => 'bool?'],
    'state.set'       => ['scope' => 'string', 'key' => 'string', 'value' => 'mixed'],
    'render.fragment' => ['target' => 'string', 'name' => 'string', 'params' => 'array?', 'fresh' => 'bool?'],
    'ui.toast'        => ['level' => 'string', 'message' => 'string'],
    // make:linker-action
]);
js
// Illustration: see actions.js for the real implementation
register('dom.scrollTo', (args) => {
    document.querySelector(args.target)?.scrollIntoView({ behavior: 'smooth' });
});

Three checks ​

  1. At registration: argument types must be string int number bool array mixed (a trailing ? means optional).
  2. When building the response: ->call('ui.toats', …) throws InvalidArgumentException in PHP. Typos blow up during development, not in a user's console.
  3. At execution: if JS lacks the action, the console warns and the pipeline counts as failed; only catch / always run after that.

CI adds a fourth check:

bash
php artisan linker:list

It lists both tables and checks they match one-to-one.

Adding an action ​

bash
php artisan make:linker-action ui.spinner target:string on:bool? --dry-run
bash
php artisan make:linker-action ui.spinner target:string on:bool?

Both sides are written at once: the PHP spec goes above the // make:linker-action marker and a JS stub is generated. If the marker is gone, the command prints the line for you to paste.

Implement the JS:

js
register('ui.spinner', (args) => {
    const el = document.querySelector(args.target);
    if (!el) return;
    el.toggleAttribute('aria-busy', args.on ?? true);
});

Use it:

php
return $this->ok()->call('ui.spinner', ['target' => '#report', 'on' => false]);

Rules for a good action ​

RuleWhy
Arguments are data, not codeNever accept JS strings, HTML strings or function names. target is only a selector
Small and generaldom.class beats ui.highlightInvoiceRow; the list should stay readable by one person
Write to the DOM with textContent / setAttributeArguments come from the server, but are always treated as data
Throw when it can failThrowing → the pipeline jumps to catch; silent skipping makes debugging harder
Return a Promise when asyncThe pipeline awaits it before the next step
Never add an eval-like actionThe moment one exists, the whole table loses its meaning

What an action receives ​

The signature is (args, context):

contextContents
elementThe element that triggered this call
hostThe @linker mount point
callLinker(method, args)Call another method on the same mount point (already holds the queue, runs immediately)

Limits ​

  • A pipeline runs at most 20 steps; extra steps are dropped with a warning.
  • A nonexistent action name fails; nothing is guessed.
  • Action JS lives in the bundle and needs a rebuild to change. That is exactly what guarantees the server can't add capabilities on the fly.

Review ​

For a PR that adds or changes an action, at least confirm:

  • [ ] Both sides exist and linker:list passes
  • [ ] No argument can be executed as code
  • [ ] DOM writes don't go through innerHTML
  • [ ] It genuinely can't be composed from existing actions