Skip to content

starfederation/datastar-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stable Version Total Downloads

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\enums\EventType;
use starfederation\datastar\enums\FragmentMergeMode;
use starfederation\datastar\ServerSentEventGenerator;

// If your framework has its own way of sending response headers, you must manually send the headers returned by `ServerSentEventGenerator::headers()` before creating a new instance of `ServerSentEventGenerator`.

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

// Merges HTML fragments into the DOM.
$sse->mergeFragments('<div></div>', [
    'selector' => '#my-div',
    'mergeMode' => FragmentMergeMode::Append,
    'settleDuration' => 1000,
    'useViewTransition' => true,
]);

// Removes HTML fragments from the DOM.
$sse->removeFragments('#my-div');

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

// Removes signals.
$sse->removeSignals(['foo', 'bar']);

// Executes JavaScript in the browser.
$sse->executeScript('console.log("Hello, world!")');

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

$signals = ServerSentEventGenerator::readSignals();

About

Datastar's Official PHP SDK

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages