Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

Component
in package

AbstractYes

Base class for server-driven components.

Public properties are the component's state. They travel to the browser in a signed snapshot and come back with each request; only properties marked #[Model] accept input from form controls, and only methods marked #[Action] can be called. Every request builds a fresh instance, so nothing is kept in the worker between requests.

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

    #[Action]
    public function increment(int $by = 1): void { $this->count += $by; }

    public function render(): mixed { return $this->view('components/counter'); }
}

Templates rendered with view() receive each property as a variable, the component as $c, and the validation errors as $errors. The framework wraps the output in the component's outer element ($tag) and adds the snapshot, so the template is just the inside.

State must be scalars, arrays of scalars, or backed enums, and is readable in the page source: keep ids in state and load the data in render().

Table of Contents

Constants

ENDPOINT  : mixed = '/_zeal/c'
REFRESH  : mixed = '_refresh'
Re-renders with the current state (and any submitted input).
UPDATE  : mixed = '_update'
Applies input and re-validates the changed fields, without an action.

Properties

$attributes  : array<string, string|bool|null>
$submit  : string|null
The action a submit of the outer form runs ($tag = 'form' only).
$tag  : string
The outer element. Use 'form' so the component works without JavaScript.
$zcBadInput  : array<int, string>
$zcErrors  : array<string, string>
$zcEvents  : array<string, mixed>
$zcFields  : array<int, string>
$zcId  : string
$zcMeta  : ComponentMeta|null
$zcRaw  : array<string, string>
$zcRedirect  : string|null
$zcTrigger  : string|null
$zcWire  : string|null

Methods

action()  : SafeHtml
Attributes that make an element (normally a <button>) run an action.
actionUrl()  : string
The endpoint URL for an action, {@see UPDATE} or {@see REFRESH}.
authorize()  : bool
Whether the current user may run this action. $action is the method name, or {@see UPDATE} / {@see REFRESH}. Return false to answer 403.
checked()  : string
checked when a bool property is true, or when an array property contains $option (checkbox groups) / a scalar property equals it (radios).
error()  : SafeHtml
The error message for a field, wrapped in a <span class="zc-error">, or nothing.
errors()  : array<string, string>
hasError()  : bool
id()  : string
The instance id, also the outer element's DOM id (e.g. counter-main).
invalidate()  : void
Mark topics stale: every open page showing a component that lists one of them in topics() re-renders it (this worker, the node's other workers, and other nodes with the Redis Store backend). Call it after the write that changed the data.
model()  : SafeHtml
Attributes that bind a form control to a #[Model] property: name, id, the current value (unless $withValue is false — for <textarea>, <select>, checkbox groups), ARIA error state, and the binder's live/blur attributes.
name()  : string
The registered component name (e.g. counter).
render()  : mixed
Produce the component's inner HTML: a string, SafeHtml, or a Generator of strings.
selected()  : string
selected when the property equals (or, for arrays, contains) $option.
tag()  : string
topics()  : array<int, string>
Topics this component depends on. When any of them is invalidated (Component::invalidate('contacts')), open pages showing this component re-render it. Needs App::componentPushRoot() in the layout.
value()  : string
A property's value as a string, HTML-escaped by default — for <textarea> content or display.
zcWire()  : string|null
The signed snapshot from the last {@see zcToHtml()} call.
addError()  : void
clearErrors()  : void
dispatch()  : void
Fire a browser event after the response is swapped in (htmx: via HX-Trigger). $detail must be JSON-encodable.
hydrate()  : void
Called on every endpoint request, after state is restored and before input is applied.
messages()  : array<string, string>
Custom messages: 'field.rule' => 'message' or 'rule' => 'message'.
mount()  : void
Called once when the component is first placed on a page, after initial props are set.
redirect()  : void
Navigate the browser to a same-site URL after this action.
rules()  : array<string, string|array<int, string|Closure>>
Extra validation rules, merged over the #[Model(rules: …)] ones.
validate()  : void
Validate #[Model] fields (all, or only $only) and throw on failure.
view()  : SafeHtml
Render a template from template/ with the component's state.
allRules()  : array<string, string|array<int, string|Closure>>
matches()  : bool
meta()  : ComponentMeta
propValue()  : mixed
runValidation()  : array<string, string>
setProp()  : void
state()  : array<string, mixed>
zcInstantiate()  : self

Constants

REFRESH

Re-renders with the current state (and any submitted input).

public mixed REFRESH = '_refresh'

UPDATE

Applies input and re-validates the changed fields, without an action.

public mixed UPDATE = '_update'

Properties

$attributes

protected array<string, string|bool|null> $attributes = []

extra attributes for the outer element

$submit

The action a submit of the outer form runs ($tag = 'form' only).

protected string|null $submit = null

$tag

The outer element. Use 'form' so the component works without JavaScript.

protected string $tag = 'div'

$zcBadInput

private array<int, string> $zcBadInput = []

fields whose submitted value couldn't be converted

$zcErrors

private array<string, string> $zcErrors = []

$zcEvents

private array<string, mixed> $zcEvents = []

$zcFields

private array<int, string> $zcFields = []

#[Model] fields the current render emitted controls for

$zcMeta

private ComponentMeta|null $zcMeta = null

$zcRaw

private array<string, string> $zcRaw = []

the raw submitted text of those fields, to show back

$zcRedirect

private string|null $zcRedirect = null

$zcTrigger

private string|null $zcTrigger = null

Methods

action()

Attributes that make an element (normally a <button>) run an action.

public action(string $action[, array<string, mixed> $args = [] ]) : SafeHtml
Parameters
$action : string
$args : array<string, mixed> = []

action arguments, by parameter name

Return values
SafeHtml

actionUrl()

The endpoint URL for an action, {@see UPDATE} or {@see REFRESH}.

public actionUrl(string $action) : string
Parameters
$action : string
Return values
string

authorize()

Whether the current user may run this action. $action is the method name, or {@see UPDATE} / {@see REFRESH}. Return false to answer 403.

public authorize(string $action) : bool
Parameters
$action : string
Return values
bool

checked()

checked when a bool property is true, or when an array property contains $option (checkbox groups) / a scalar property equals it (radios).

public checked(string $prop[, mixed $option = true ]) : string
Parameters
$prop : string
$option : mixed = true
Return values
string

error()

The error message for a field, wrapped in a <span class="zc-error">, or nothing.

public error(string $field) : SafeHtml
Parameters
$field : string
Return values
SafeHtml

errors()

public errors() : array<string, string>
Return values
array<string, string> —

field → message

hasError()

public hasError(string $field) : bool
Parameters
$field : string
Return values
bool

id()

The instance id, also the outer element's DOM id (e.g. counter-main).

public id() : string
Return values
string

invalidate()

Mark topics stale: every open page showing a component that lists one of them in topics() re-renders it (this worker, the node's other workers, and other nodes with the Redis Store backend). Call it after the write that changed the data.

public static invalidate(string ...$topics) : void
Parameters
$topics : string

model()

Attributes that bind a form control to a #[Model] property: name, id, the current value (unless $withValue is false — for <textarea>, <select>, checkbox groups), ARIA error state, and the binder's live/blur attributes.

public model(string $prop[, bool $withValue = true ]) : SafeHtml
Parameters
$prop : string
$withValue : bool = true
Return values
SafeHtml

name()

The registered component name (e.g. counter).

public name() : string
Return values
string

render()

Produce the component's inner HTML: a string, SafeHtml, or a Generator of strings.

public abstract render() : mixed

selected()

selected when the property equals (or, for arrays, contains) $option.

public selected(string $prop, mixed $option) : string
Parameters
$prop : string
$option : mixed
Return values
string

tag()

public tag() : string
Return values
string

topics()

Topics this component depends on. When any of them is invalidated (Component::invalidate('contacts')), open pages showing this component re-render it. Needs App::componentPushRoot() in the layout.

public topics() : array<int, string>

Topics: 1-128 letters, digits, :, _, . or - (e.g. contact:42).

Return values
array<int, string>

value()

A property's value as a string, HTML-escaped by default — for <textarea> content or display.

public value(string $prop[, bool $escape = true ]) : string
Parameters
$prop : string
$escape : bool = true
Return values
string

zcWire()

The signed snapshot from the last {@see zcToHtml()} call.

public final zcWire() : string|null

@internal

Return values
string|null

addError()

protected addError(string $field, string $message) : void
Parameters
$field : string
$message : string

clearErrors()

protected clearErrors([string|null $field = null ]) : void
Parameters
$field : string|null = null

dispatch()

Fire a browser event after the response is swapped in (htmx: via HX-Trigger). $detail must be JSON-encodable.

protected dispatch(string $event[, mixed $detail = null ]) : void
Parameters
$event : string
$detail : mixed = null

hydrate()

Called on every endpoint request, after state is restored and before input is applied.

protected hydrate() : void

messages()

Custom messages: 'field.rule' => 'message' or 'rule' => 'message'.

protected messages() : array<string, string>

:field is replaced with the field's label.

Return values
array<string, string>

mount()

Called once when the component is first placed on a page, after initial props are set.

protected mount() : void

redirect()

Navigate the browser to a same-site URL after this action.

protected redirect(string $url) : void
Parameters
$url : string

rules()

Extra validation rules, merged over the #[Model(rules: …)] ones.

protected rules() : array<string, string|array<int, string|Closure>>
Return values
array<string, string|array<int, string|Closure>>

validate()

Validate #[Model] fields (all, or only $only) and throw on failure.

protected validate([array<int, string>|null $only = null ]) : void

Errors for the checked fields are replaced; others are kept.

Parameters
$only : array<int, string>|null = null
Tags
throws
ValidationException

view()

Render a template from template/ with the component's state.

protected view(string $template[, array<string, mixed> $vars = [] ]) : SafeHtml
Parameters
$template : string
$vars : array<string, mixed> = []

extra variables

Return values
SafeHtml

allRules()

private allRules() : array<string, string|array<int, string|Closure>>
Return values
array<string, string|array<int, string|Closure>>

matches()

private matches(string $prop, mixed $option) : bool
Parameters
$prop : string
$option : mixed
Return values
bool

meta()

private meta() : ComponentMeta
Return values
ComponentMeta

propValue()

private propValue(string $prop) : mixed
Parameters
$prop : string

runValidation()

private runValidation(array<int, string>|null $only) : array<string, string>
Parameters
$only : array<int, string>|null
Return values
array<string, string> —

errors for the checked fields

setProp()

private setProp(string $prop, mixed $value) : void
Parameters
$prop : string
$value : mixed

state()

private state() : array<string, mixed>
Return values
array<string, mixed>

zcInstantiate()

private static zcInstantiate(ComponentMeta $meta, string $id) : self
Parameters
$meta : ComponentMeta
$id : string
Return values
self
On this page