Download the PHP package yuga/livecomponent without Composer
On this page you can find all versions of the php package yuga/livecomponent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yuga/livecomponent
More information about yuga/livecomponent
Files in yuga/livecomponent
Package livecomponent
Short Description Live components for the Yuga Framework.
License MIT
Informations about the package livecomponent
Yuga Live Components
Yuga Live Components (YLC) adds server-rendered, reactive components to the Yuga Framework. Component state stays on the server and is synchronized in the browser through the Yuga JavaScript runtime.
Requirements
- PHP 8.2, 8.3, or 8.4
- Yuga Framework 5.x
- The Yuga JavaScript runtime
Installation
Install the package with Composer:
YlcProvider is registered through Composer package discovery. It adds the Live Component endpoints and discovers components configured by the application.
Publish or copy the browser plugin into the application's public assets using the Yuga publishing mechanism and the ylc-assets tag. The package asset is public/plugins/ylc-live-plugin.js.
Register YLCPlugin with the Yuga JavaScript runtime after loading the runtime and the published plugin. Use the normal Yuga plugin registration API used by the application:
The plugin communicates with these routes, which are registered automatically by YlcProvider:
| Method | Route | Purpose |
|---|---|---|
| POST | /ylc/message |
Component updates and actions |
| POST | /ylc/lazy |
Lazy component loading |
| GET | /ylc/stream |
Stream refresh notifications |
| POST | /ylc/upload |
Temporary file uploads |
| GET | /ylc/temp-upload/{token} |
Temporary upload preview |
Configuration
Add a ylc configuration entry to the Yuga application's configuration. Each discovery source needs a filesystem path and the namespace used by the PHP classes:
Components are named from their class path. For example, App\Live\Shopping\Cart becomes shopping.cart. Set #[Live(name: 'cart')] to choose an explicit name.
Creating a component
Create a class that extends Yuga\Live\Component:
Public properties are included in the component snapshot and can be bound from the browser. Public methods can be invoked with ylc:click.
To disable discovery for a class, use #[Live(discover: false)]. Discovery options can also be set on the attribute:
Mounting a component
Mount a component from a Yuga view with the ylc helper:
Pass parameters to mount() and per-instance options as the second and third arguments:
The component receives mount parameters in mount():
Bindings and actions
Use ylc:model for state bindings and ylc:click for actions:
Supported model values include text inputs, checkboxes, radios, multiple selects, numeric inputs, ranges, and file inputs. Model updates are sent to the server automatically.
Loading state can also be reflected in attributes:
Action parameters use JavaScript expressions inside the attribute, for example remove(42, 'archive').
Forms and validation
Typed public properties extending Form are initialized automatically. Define the form fields and validation rules in a form class:
Use it on a component and bind nested fields with dot notation:
URL state
Mark a public property with #[Url] to synchronize it with the query string:
Array properties are JSON-encoded in the query string. URL-backed state is restored on the initial request and when the browser history changes.
Computed and locked properties
Computed properties are exposed through a get<Name>Property() method:
Use #[Computed(cache: false)] when the value must be recalculated each time it is read. Use #[Locked] on a public property when browser updates must not change it.
JavaScript API
The plugin exposes components through window.YLC:
Other helpers include YLC.find(id), YLC.all(), and component.refresh().
Register named JavaScript actions from application code:
Then queue one from a component with $this->js('notify', ['message' => 'Saved']);.
Component lifecycle
Components may define these hooks:
Use stream: true for server-sent refresh notifications or poll: true for periodic refreshes. The client falls back to polling after repeated stream failures when fallback is enabled.
License
Yuga Live Components is open-sourced under the MIT license.