Code Monkey home page Code Monkey logo

htmx-message's Introduction

htmx-message - PHP PSR-7 proxy classes with convenience methods for htmx requests and responses

PHP Version Require Latest Stable Version License

PSR-7 ServerRequestInterface and ResponseInterface proxy classes for htmx with convenience methods for all special htmx headers (HX-*) in request and response objects.

HtmxResponse is a proxy for PSR-7 response objects implementing \Psr\Http\Message\ResponseInterface.

HtmxServerRequest is a proxy for PSR-7 server request objects implementing \Psr\Http\Message\ServerRequestInterface.

You can safely wrap any ResponseInterface or ServerRequestInterface object, also those not originating from htmx, without affecting your application.

HtmxResponse and HtmxServerRequest objects retains immutability in the same fashion as one would expect from a PSR-7 MessageInterface, returning a new object wrapping a new ResponseInterface instance whenever the HtmxResponse is changed.

Conforms to htmx version 1.9.x (latest) as per 2023-08-01.

๐Ÿ“” Go to documentation

Installation

Installation via composer:

composer require tomrf/htmx-message

Usage

$request = new HtmxRequest($request);     // PSR-7 `ServerRequestInterface`

if ($request->isHxRequest() && $request->isHxBoosted()) {
    $layout = 'ajax.layout';
}

[...]

$response = new HtmxResponse($response);  // PSR-7 `ResponseInterface`

$response = $response->withHxTrigger('aSimpleTrigger')
    ->withAddedHxTrigger('triggerWithParams', ['arg' => true, 'arg2' => 7])
    ->withTriggerAfterSwap('afterSwap', time())
    ->withHxPush($newUrl);

// Emit $response as normal
[...]

Testing

composer test

License

This project is released under the MIT License (MIT). See LICENSE for more information.

Documentation


๐Ÿ“‚ Tomrf\HtmxMessage\HtmxResponse::class

HtmxResponse is a proxy for PSR-7 response objects implementing \Psr\Http\Message\ResponseInterface.

You can safely wrap any ResponseInterface object, also those not responding to a htmx request, without affecting your application.

HtmxResponse objects retains immutability in the same fashion as one would expect from a PSR-7 MessageInterface, returning a new object wrapping a new ResponseInterface instance whenever the HtmxResponse is changed.

Example:

$response = new HtmxResponse($response);
$response = $response->withHxTrigger('myTrigger')
    ->withRedirect('/user/redirected')
    ->withHxPush(false);

// ...

getHxPush()

public function getHxPush(): string

getHxRedirect()

public function getHxRedirect(): string

getHxRetarget()

public function getHxRetarget(): string

getHxTrigger()

public function getHxTrigger(): array

@return   array

getHxTriggerAfterSettle()

public function getHxTriggerAfterSettle(): array

@return   array

getHxTriggerAfterSwap()

public function getHxTriggerAfterSwap(): array

@return   array

hasHxPush()

public function hasHxPush(): bool

hasHxRedirect()

public function hasHxRedirect(): bool

hasHxRefresh()

public function hasHxRefresh(): bool

hasHxRetarget()

public function hasHxRetarget(): bool

hasHxTrigger()

public function hasHxTrigger(): bool

hasHxTriggerAfterSettle()

public function hasHxTriggerAfterSettle(): bool

hasHxTriggerAfterSwap()

public function hasHxTriggerAfterSwap(): bool

withAddedHxTrigger()

public function withAddedHxTrigger(
    string $trigger,
    mixed $argument = null
): static

withAddedHxTriggerAfterSettle()

public function withAddedHxTriggerAfterSettle(
    string $trigger,
    mixed $argument = null
): static

withAddedHxTriggerAfterSwap()

public function withAddedHxTriggerAfterSwap(
    string $trigger,
    mixed $argument = null
): static

withHxPush()

public function withHxPush(
    string|bool $url
): static

withHxRedirect()

public function withHxRedirect(
    string $url
): static

withHxRefresh()

public function withHxRefresh(): static

withHxRetarget()

public function withHxRetarget(
    string $selector
): static

withHxTrigger()

public function withHxTrigger(
    string $trigger,
    mixed $argument = null
): static

withHxTriggerAfterSettle()

public function withHxTriggerAfterSettle(
    string $trigger,
    mixed $argument = null
): static

withHxTriggerAfterSwap()

public function withHxTriggerAfterSwap(
    string $trigger,
    mixed $argument = null
): static

withoutHxPush()

public function withoutHxPush(): static

withoutHxRedirect()

public function withoutHxRedirect(): static

withoutHxRefresh()

public function withoutHxRefresh(): static

withoutHxRetarget()

public function withoutHxRetarget(): static

withoutHxTrigger()

public function withoutHxTrigger(): static

withoutHxTriggerAfterSettle()

public function withoutHxTriggerAfterSettle(): static

withoutHxTriggerAfterSwap()

public function withoutHxTriggerAfterSwap(): static

๐Ÿ“‚ Tomrf\HtmxMessage\HtmxServerRequest::class

HtmxServerRequest is a proxy for PSR-7 server request objects implementing \Psr\Http\Message\ServerRequestInterface.

You can safely wrap any ServerRequestInterface object, also those not originating from htmx, without affecting your application.

Check if the request originated from htmx using the isHxRequest() method.

HtmxServerRequest objects retains immutability in the same fashion as one would expect from a PSR-7 MessageInterface, returning a new object wrapping a new ServerRequestInterface instance whenever the HtmxServerRequest is changed.

Example:

$request = new HtmxServerRequest($request);
if ($request->isHxRequest() && $request->isHxBoosted()) {
    // htmx request from boosted client, respond accordingly
}

$userPrompt = $request->getHxPrompt();
// ...

getHxCurrentUrl()

public function getHxCurrentUrl(): string

getHxPrompt()

public function getHxPrompt(): string

getHxTarget()

public function getHxTarget(): string

getHxTrigger()

public function getHxTrigger(): string

getHxTriggerName()

public function getHxTriggerName(): string

hasHxPrompt()

public function hasHxPrompt(): bool

hasHxTarget()

public function hasHxTarget(): bool

hasHxTrigger()

public function hasHxTrigger(): bool

hasHxTriggerName()

public function hasHxTriggerName(): bool

isHxBoosted()

public function isHxBoosted(): bool

isHxHistoryRestoreRequest()

public function isHxHistoryRestoreRequest(): bool

isHxRequest()

public function isHxRequest(): bool

__construct()

public function __construct(
    Psr\Http\Message\ServerRequestInterface $message
): void

Generated 2023-08-18T12:46:49+00:00 using ๐Ÿ“štomrf/readme-gen

htmx-message's People

Contributors

tomrf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

htmx-message's Issues

Example usage

Thank you for building this package.

It would be great if there some code examples of implementing this middleware.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.