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
| Side | Where | Contents |
|---|---|---|
| PHP | LinkerServiceProvider::registerActions() | Action name + argument spec |
| JS | resources/js/linker/actions.js | Action 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
- At registration: argument types must be
stringintnumberboolarraymixed(a trailing?means optional). - When building the response:
->call('ui.toats', …)throwsInvalidArgumentExceptionin PHP. Typos blow up during development, not in a user's console. - At execution: if JS lacks the action, the console warns and the pipeline counts as failed; only
catch/alwaysrun after that.
CI adds a fourth check:
bash
php artisan linker:listIt 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-runbash
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
| Rule | Why |
|---|---|
| Arguments are data, not code | Never accept JS strings, HTML strings or function names. target is only a selector |
| Small and general | dom.class beats ui.highlightInvoiceRow; the list should stay readable by one person |
Write to the DOM with textContent / setAttribute | Arguments come from the server, but are always treated as data |
| Throw when it can fail | Throwing → the pipeline jumps to catch; silent skipping makes debugging harder |
| Return a Promise when async | The pipeline awaits it before the next step |
Never add an eval-like action | The moment one exists, the whole table loses its meaning |
What an action receives
The signature is (args, context):
context | Contents |
|---|---|
element | The element that triggered this call |
host | The @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:listpasses - [ ] No argument can be executed as code
- [ ] DOM writes don't go through
innerHTML - [ ] It genuinely can't be composed from existing actions