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

LikeCounter extends Component
in package

FinalYes

Reactive components demo: a counter whose state lives in the page's signed snapshot. Each click posts to /_zeal/c/like-counter/{action} and swaps the re-rendered component in.

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

$count  : int
$attributes  : array<string|int, mixed>
$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.

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}.
add()  : void
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.
reset()  : void
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.

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|int, mixed> $attributes = ['class' => 'lc-demo lc-counter']

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 = 'form'

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

add()

public add([int $by = 1 ]) : void
Parameters
$by : int = 1
Attributes
#[Action]

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 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
On this page