百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-htmx

> 编程语言
开源

用于 Htmx 的 Laravel 帮助函数库

363 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 Htmx 的 Laravel 帮助函数库

laravel-htmx

Laravel integration for htmx. Supported Laravel Versions >= v8.80.0.

Installation

You can install the package via composer:

composer require mauricius/laravel-htmx

To install htmx please browse their documentation

Usage

Request

You can resolve an instance of the HtmxRequest from the container which provides shortcuts for reading the htmx-specific request headers.

…

Response

  • HtmxResponseClientRedirect

htmx can trigger a client side redirect when it receives a response with the HX-Redirect header. The HtmxResponseClientRedirect makes it easy to trigger such redirects.

use Mauricius\LaravelHtmx\Http\HtmxResponseClientRedirect;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseClientRedirect('/somewhere-else');
});
  • HtmxResponseClientRefresh

htmx will trigger a page reload when it receives a response with the HX-Refresh header. HtmxResponseClientRefresh is a custom response class that allows you to send such a response. It takes no arguments, since htmx ignores any content.

use Mauricius\LaravelHtmx\Http\HtmxResponseClientRefresh;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseClientRefresh();
});
  • HtmxResponseStopPolling

When using a polling trigger, htmx will stop polling when it encounters a response with the special HTTP status code 286. HtmxResponseStopPolling is a custom response class with that status code.

use Mauricius\LaravelHtmx\Http\HtmxResponseStopPolling;

Route::get('/', function (HtmxRequest $request)
{
    return new HtmxResponseStopPolling();
});

For all the remaining available headers you can use the HtmxResponse class.

…

Additionally, you can trigger client-side events using the addTrigger methods.

use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse())
        ->addTrigger("myEvent")
        ->addTriggerAfterSettle("myEventAfterSettle")
        ->addTriggerAfterSwap("myEventAfterSwap");
});

If you want to pass details along with the event you can use the second argument to send a body. It supports strings or arrays.

use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse()
        ->addTrigger("showMessage", "Here Is A Message")
        ->addTriggerAfterSettle("showAnotherMessage", [
            "level" => "info",
            "message" => "Here Is A Message"
        ]);
});

You can call those methods multiple times if you want to trigger multiple events.

use Mauricius\LaravelHtmx\Http\HtmxResponse;

Route::get('/', function (HtmxRequest $request)
{
    return (new HtmxResponse())
        ->addTrigger("event1", "A Message")
        ->addTrigger("event2", "Another message");
});

Render Blade Fragments

This library also provides a basic Blade extension to render template fragments.

The library provides two new Blade directives: @fragment and @endfragment. You can use these directives to specify a block of content within a template and render just that bit of content. For instance:

{{-- /contacts/detail.blade.php  --}}

    
        

        
Contact

        
{{ $contact->email }}

    

With this fragment defined in our template, we can now render either the entire template:

Route::get('/', function ($id) {
    $contact = Contact::find($id);

    return View::make('contacts.detail', compact('contact'));
});

Or we can render only the archive-ui fragment of the template by using the renderFragment macro defined in the \Illuminate\View\View class:

…

OOB Swap support

htmx supports updating multiple targets by returning multiple partial responses with hx-swap-oob. With this library you can return multiple fragments by using the HtmxResponse as a return type.

For instance, let's say that we want to mark a todo as completed using a PATCH request to /todos/{id}. With the same request, we also want to update in the footer how many todos are left:

…

We can use the HtmxResponse to return multiple fragments:

Route::patch('/todos/{id}', function ($id) {
    $todo = Todo::find($id);
    $todo->done = !$todo->done;
    $todo->save();

    $left = Todo::where('done', 0)->count();

    return HtmxResponse::addFragment('todomvc', 'todo', compact('todo'))
        ->addFragment('todomvc', 'todo-count', compact('left'));
});

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

  • mauricius
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言