承接 magehx/htmx-actions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

magehx/htmx-actions

Composer 安装命令:

composer require magehx/htmx-actions

包简介

Magento 2 module that provides a base controller to manage HTMX requests.

README 文档

README

A Magento 2 module that enhances HTMX integration by providing structured controllers, events, view models, services, and enums to streamline dynamic HTML responses.

An HTMX request expects an HTML snippet in response, which will be dynamically swapped into the frontend page.

This module provides many utilities to ease this process.

It offers a convenient base for handling common HTMX response scenarios, allowing you to return HTML fragments efficiently and consistently.

✨ Features

  • Abstract Controller: Simplifies handling HTMX requests with a base controller.
  • Event Dispatching: Fires events during the HTMX request lifecycle for extensibility.
  • View Models: Provides view models to assist in rendering HTMX attributes in templates.
  • Header Management Service: Offers a service to manage HTMX-specific response headers.
  • Enumerations: Defines enums for HTMX attributes and headers to ensure consistency.
  • HTMX Attribute Renderers: Includes traits for rendering HTMX attributes in templates.

📦 Installation

Install the module via Composer:

composer require magehx/htmx-actions

🔧 Usage

Abstract Controller

Extend the MageHx\HtmxActions\Controller\HtmxAction to handle HTMX requests:

  • getBlockResponse() - This allows to retrieve a block response from the given layout handles.
  • getMultiBlockResponse() - This allows to retrieve combined blocks response from the given layout handles.
  • getEmptyResponse() - Gives empty response. Useful when you decided to not swap the content in the frontend.
use MageHx\HtmxActions\Controller\HtmxAction;

class Example extends HtmxAction
{
    public function execute(): \Magento\Framework\Controller\ResultInterface
    {
        return $this->getBlockResponse('your.block.name', ['your_layout_handle']);
    }
}

Event Dispatching

The controller dispatches useful events during the HTMX request lifecycle:

  • hxactions_block_response_before: Fires before preparing the block response. Useful to add more layout handles or change the block itself.
  • hxactions_block_response_after: Fires after preparing the block response. Useful to change the response.
  • hxactions_multi_blocks_response_before: Fires before preparing the multi block response.
  • hxactions_multi_blocks_response_after: Fires after preparing the multi block response.

Observers can be created to listen to these events for custom logic.

View Models

MageHx\HtmxActions\ViewModel\HxAttributesRenderer provides many convenient ways to render HTMX attributes on HTML elements

<?php
/** @var MageHx\HtmxActions\ViewModel\HxAttributesRenderer $hxAttrRenderer */
?>

<button 
    <?= $hxAttrRenderer->post('/submit') ?>
    <?= $hxAttrRenderer->target('#result') ?>
>Submit</button>

The outputs will look like this:

<button hx-post="/submit" hx-target="#result">Submit</button>

You can also chain attributes using the hxBuilder() method for rendering HTMX attributes. Example:

<?php
/** @var MageHx\HtmxActions\ViewModel\HxAttributesRenderer $hxAttrRenderer */
?>

<button 
    <?= $hxAttrRenderer->hxBuilder()
        ->post('/submit')
        ->target('#result')
        ->render() ?>
>Submit</button>

This will also give you the same result as in the first case.

There are many other methods supported like get, indicator, swap and many more.

Header Management Service

Utilize the MageHx\HtmxActions\Service\HtmxHeaderReader to manage HTMX request/response headers:

// to verify the request is an HTMX request.
$manager->isHtmxRequest(); 
// Here you can set a header value to the response.
$manager->setHeader($response, \MageHx\HtmxActions\Enums\HtmxResponseHeader::RESWAP, \MageHx\HtmxActions\Enums\HtmxSwapOption::NONE->value);

and there are many more such useful header methods available there.

Enumerations

The module defines enums for HTMX attributes and headers:

  • HtmxCoreAttributes: Includes get, post, target, swap, trigger, etc.
  • HtmxAdditionalAttributes: Includes boost, indicator, etc.
  • HtmxHeaders: Includes HX-Trigger, HX-Redirect, HX-Refresh, etc.

These enums ensure consistency and reduce typos in attribute names.

🔗 Requirements

  • Magento 2.4.0+
  • PHP 8.1+

🛠️ License

This module is open-sourced software licensed under the MIT license.

💬 Feedback / Contributions

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

magehx/htmx-actions 适用场景与选型建议

magehx/htmx-actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 443 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 magehx/htmx-actions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 magehx/htmx-actions 我们能提供哪些服务?
定制开发 / 二次开发

基于 magehx/htmx-actions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 443
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 19
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-10