ecoleplus/htmx-bridge
Composer 安装命令:
composer require ecoleplus/htmx-bridge
包简介
A strict PSR-7/PSR-15 HTMX Bridge for PHP 8.3+
README 文档
README
A strict PSR-7 and PSR-15 compliant bridge for integrating HTMX with modern PHP 8.3+ applications.
This library provides type-safe Decorators and Middleware to easily inspect HTMX requests and generate HTMX-aware responses without coupling your domain logic to specific framework implementations.
Features
- Strictly Typed: Utilizes PHP 8.3+ typing for robustness.
- PSR-7 Immutability: Fully respects HTTP Message immutability conventions.
- PSR-15 Middleware: Plug-and-play integration with Mezzio, Slim, Laravel, or any PSR-15 compliant dispatcher.
- Event Merging: Intelligently merges multiple HTMX triggers (
HX-Trigger) into a single JSON header. - Comprehensive Coverage: Supports advanced HTMX headers like
HX-Boosted,HX-Trigger-After-Swap,HX-Push-Url, and more.
Installation
composer require ecoleplus/htmx-bridge
Usage
1. Register Middleware
Add EcolePlus\HtmxBridge\Middleware\HtmxRequestMiddleware to your middleware pipeline early in the stack. This will decorate the incoming ServerRequestInterface with HTMX capabilities.
// Example in a Mezzio/Laminas pipeline
$app->pipe(EcolePlus\HtmxBridge\Middleware\HtmxRequestMiddleware::class);
// Example in Slim Framework
$app->add(new EcolePlus\HtmxBridge\Middleware\HtmxRequestMiddleware());
2. Inspecting Requests
In your controller or request handler, you can check if the request is an HTMX request and retrieve metadata.
use EcolePlus\HtmxBridge\Contract\HtmxRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
public function handle(ServerRequestInterface $request): ResponseInterface
{
// 1. Check if the request object implements our interface
if ($request instanceof HtmxRequestInterface && $request->isHtmx()) {
// 2. Access HTMX specific data
$targetId = $request->getTargetId();
$triggeredBy = $request->getTriggerId();
// ... application logic for partial rendering ...
}
// ... standard full-page render logic ...
}
3. Constructing Responses
Use the HtmxResponseDecorator to build HTMX-aware responses fluently. Since it is immutable, remember to chain methods or capture the return value.
use EcolePlus\HtmxBridge\Decorator\HtmxResponseDecorator;
use Laminas\Diactoros\Response\JsonResponse;
// Create a standard PSR-7 response
$response = new JsonResponse(['status' => 'ok']);
// Wrap and decorate
return (new HtmxResponseDecorator($response))
->withPushUrl('/new-url') // Update browser URL
->withTrigger('userCreated', ['id' => 1]) // Client-side event
->withTriggerAfterSwap('showToast', []) // Event after swap
->withReswap('innerHTML') // HTMX swap strategy
->getResponse(); // Unwrap back to PSR-7 Response
API Reference
Request (HtmxRequestInterface)
isHtmx(): bool- Returns true ifHX-Requestis present.isBoosted(): bool- Returns true ifHX-Boostedis present.isHistoryRestoreRequest(): bool- Returns true if restoring history.getCurrentUrl(): ?string- ReturnsHX-Current-Url.getPrompt(): ?string- Returns user input fromHX-Prompt.getTargetId(): ?string- ReturnsHX-TargetID.getTriggerId(): ?string- ReturnsHX-Triggerelement ID.getTriggerName(): ?string- ReturnsHX-Trigger-Name.
Response (HtmxResponseInterface)
All methods are immutable and return a new instance.
withPushUrl(string|false $url)- Updates browser history.withReplaceUrl(string|false $url)- Replaces current URL.withReswap(string $value)- Sets swap method (e.g.,innerHTML).withRetarget(string $selector)- Changes update target.withRefresh()- Triggers a full page refresh.withRedirect(string $url)- Client-side redirect via HTMX.withTrigger(string $event, array $data = [])- Triggers event immediately.withTriggerAfterSwap(string $event, array $data = [])- Triggers event after swap.withTriggerAfterSettle(string $event, array $data = [])- Triggers event after settle.
License
MIT License
ecoleplus/htmx-bridge 适用场景与选型建议
ecoleplus/htmx-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ecoleplus/htmx-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ecoleplus/htmx-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-25