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 via composer:
composer require tomrf/htmx-message
$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
[...]
composer test
This project is released under the MIT License (MIT). See LICENSE for more information.
- Tomrf\HtmxMessage\HtmxResponse
- getHxPush
- getHxRedirect
- getHxRetarget
- getHxTrigger
- getHxTriggerAfterSettle
- getHxTriggerAfterSwap
- hasHxPush
- hasHxRedirect
- hasHxRefresh
- hasHxRetarget
- hasHxTrigger
- hasHxTriggerAfterSettle
- hasHxTriggerAfterSwap
- withAddedHxTrigger
- withAddedHxTriggerAfterSettle
- withAddedHxTriggerAfterSwap
- withHxPush
- withHxRedirect
- withHxRefresh
- withHxRetarget
- withHxTrigger
- withHxTriggerAfterSettle
- withHxTriggerAfterSwap
- withoutHxPush
- withoutHxRedirect
- withoutHxRefresh
- withoutHxRetarget
- withoutHxTrigger
- withoutHxTriggerAfterSettle
- withoutHxTriggerAfterSwap
- Tomrf\HtmxMessage\HtmxServerRequest
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);
// ...
public function getHxPush(): string
public function getHxRedirect(): string
public function getHxRetarget(): string
public function getHxTrigger(): array
@return array
public function getHxTriggerAfterSettle(): array
@return array
public function getHxTriggerAfterSwap(): array
@return array
public function hasHxPush(): bool
public function hasHxRedirect(): bool
public function hasHxRefresh(): bool
public function hasHxRetarget(): bool
public function hasHxTrigger(): bool
public function hasHxTriggerAfterSettle(): bool
public function hasHxTriggerAfterSwap(): bool
public function withAddedHxTrigger(
string $trigger,
mixed $argument = null
): static
public function withAddedHxTriggerAfterSettle(
string $trigger,
mixed $argument = null
): static
public function withAddedHxTriggerAfterSwap(
string $trigger,
mixed $argument = null
): static
public function withHxPush(
string|bool $url
): static
public function withHxRedirect(
string $url
): static
public function withHxRefresh(): static
public function withHxRetarget(
string $selector
): static
public function withHxTrigger(
string $trigger,
mixed $argument = null
): static
public function withHxTriggerAfterSettle(
string $trigger,
mixed $argument = null
): static
public function withHxTriggerAfterSwap(
string $trigger,
mixed $argument = null
): static
public function withoutHxPush(): static
public function withoutHxRedirect(): static
public function withoutHxRefresh(): static
public function withoutHxRetarget(): static
public function withoutHxTrigger(): static
public function withoutHxTriggerAfterSettle(): static
public function withoutHxTriggerAfterSwap(): static
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();
// ...
public function getHxCurrentUrl(): string
public function getHxPrompt(): string
public function getHxTarget(): string
public function getHxTrigger(): string
public function getHxTriggerName(): string
public function hasHxPrompt(): bool
public function hasHxTarget(): bool
public function hasHxTrigger(): bool
public function hasHxTriggerName(): bool
public function isHxBoosted(): bool
public function isHxHistoryRestoreRequest(): bool
public function isHxRequest(): bool
public function __construct(
Psr\Http\Message\ServerRequestInterface $message
): void
Generated 2023-08-18T12:46:49+00:00 using πtomrf/readme-gen