starfederation/datastar-php

A PHP SDK for working with Datastar.

Maintainers

👁 putyourlightson

Package info

github.com/starfederation/datastar-php

Documentation

pkg:composer/starfederation/datastar-php

Statistics

Installs: 31 454

Dependents: 7

Suggesters: 0

Stars: 41

Open Issues: 0

1.0.1 2026-06-02 18:56 UTC

Requires

  • php: >=8.1

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

mit a02c72956c9ee80fe7ae2ce28d1112cf92c6ffbf


README

👁 Stable Version
👁 Total Downloads

👁 Image

Datastar PHP SDK

This package provides a PHP SDK for working with Datastar.

License

This package is licensed for free under the MIT License.

Requirements

This package requires PHP 8.1 or later.

Installation

Install using composer.

composer require starfederation/datastar-php

Usage

use starfederation\datastar\ServerSentEventGenerator;

// Reads signals from the request.
$signals = ServerSentEventGenerator::readSignals();
use starfederation\datastar\enums\EventType;
use starfederation\datastar\enums\ElementPatchMode;
use starfederation\datastar\ServerSentEventGenerator;

// Creates a new `ServerSentEventGenerator` instance.
$sse = new ServerSentEventGenerator();

// Sends the response headers. 
// If your framework has its own way of sending response headers, manually send the headers returned by `ServerSentEventGenerator::headers()` instead.
$sse->sendHeaders();

// Patches elements into the DOM.
$sse->patchElements('<div></div>', [
 'selector' => '#my-div',
 'mode' => ElementPatchMode::Append,
 'useViewTransition' => true,
]);

// Removes elements from the DOM.
$sse->removeElements('#my-div', [
 'useViewTransition' => true,
]);

// Patches signals.
$sse->patchSignals('{foo: 123}', [
 'onlyIfMissing' => true,
]);

// Executes JavaScript in the browser.
$sse->executeScript('console.log("Hello, world!")', [
 'autoRemove' => true,
 'attributes' => [
 'type' => 'application/javascript',
 ],
]);

// Redirects the browser by setting the location to the provided URI.
$sse->location('/guide');