用于 Htmx 的 Laravel 帮助函数库
Laravel integration for htmx. Supported Laravel Versions >= v8.80.0.
You can install the package via composer:
composer require mauricius/laravel-htmx
To install htmx please browse their documentation
You can resolve an instance of the HtmxRequest from the container which provides shortcuts for reading the htmx-specific request headers.
…
HtmxResponseClientRedirecthtmx 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');
});
HtmxResponseClientRefreshhtmx 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();
});
HtmxResponseStopPollingWhen 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");
});
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:
…
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'));
});
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
暂无开放 Issues,或尚未同步最近议题。