-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge-htmx.html
More file actions
968 lines (911 loc) · 47.4 KB
/
Copy pathforge-htmx.html
File metadata and controls
968 lines (911 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
<!doctype html>
<html lang="en" class="scroll-smooth">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Forge Kernel - ForgeHtmx</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css"
rel="stylesheet"
/>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="assets/css/docs.css" />
</head>
<body class="bg-gray-50 text-gray-900">
<!-- Navigation -->
<nav class="bg-white shadow-sm border-b">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex items-center">
<div class="flex-shrink-0">
<h1 class="text-xl font-bold text-gray-900">
Forge Kernel
</h1>
</div>
<div class="hidden sm:ml-6 sm:flex sm:space-x-8">
<a
href="index.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Home
</a>
<a
href="getting-started.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Getting Started
</a>
<a
href="core-concepts.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Core Concepts
</a>
<a
href="modules.html"
class="text-blue-600 inline-flex items-center px-1 pt-1 text-sm font-medium border-b-2 border-blue-600"
>
Capabilities
</a>
<a
href="api-reference.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
API Reference
</a>
<a
href="tutorial.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Tutorials
</a>
</div>
</div>
<div class="flex items-center sm:hidden">
<button
id="mobile-menu-button"
class="text-gray-700 hover:text-blue-600 p-2"
>
<i class="fas fa-bars text-xl"></i>
</button>
</div>
</div>
</div>
<div
id="mobile-menu"
class="sm:hidden hidden bg-white border-t border-gray-200"
>
<div class="px-2 pt-2 pb-3 space-y-1">
<a
href="index.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Home</a
>
<a
href="getting-started.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Getting Started</a
>
<a
href="core-concepts.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Core Concepts</a
>
<a
href="modules.html"
class="block px-3 py-2 text-blue-600 font-medium"
>Capabilities</a
>
<a
href="api-reference.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>API Reference</a
>
<a
href="tutorial.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Tutorials</a
>
</div>
</div>
</nav>
<!-- Main Content -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex flex-col lg:flex-row gap-8">
<!-- Sidebar Navigation -->
<div id="sidebar-nav" class="lg:w-1/4">
<div class="bg-white rounded-lg shadow-sm p-6 sticky top-8">
<h3 class="text-lg font-semibold text-gray-900 mb-4">
ForgeHtmx
</h3>
<nav class="space-y-2">
<a
href="#overview"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Overview</a
>
<a
href="#installation"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Installation</a
>
<a
href="#basic-usage"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Basic Usage</a
>
<a
href="#response-helpers"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Response Helpers</a
>
<a
href="#htmx-view"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>HtmxView Helper</a
>
<a
href="#middleware"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Middleware</a
>
<a
href="#examples"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Common Examples</a
>
<a
href="#under-the-hood"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Under the Hood</a
>
</nav>
</div>
</div>
<!-- Main Content -->
<div class="lg:w-3/4">
<div class="bg-white rounded-lg shadow-sm p-8">
<h1 class="text-4xl font-bold text-gray-900 mb-6">
ForgeHtmx
</h1>
<p class="text-xl text-gray-600 mb-8">
HTMX integration for Forge Kernel. Add AJAX-powered
partial updates, triggers, and dynamic navigation to
your Forge applications with minimal JavaScript.
</p>
<!-- Overview -->
<section id="overview" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
What It Is
</h2>
<p class="text-gray-600 mb-6">
ForgeHtmx gives Forge controllers first-class
HTMX support. Use traits to respond with HTMX
headers, render partial views conditionally, and
protect your HTMX endpoints with automatic CSRF
token injection.
</p>
<div class="bg-indigo-50 border border-indigo-200 rounded-lg p-6 mb-6">
<p class="text-indigo-900 text-sm font-semibold mb-2">Philosophy</p>
<p class="text-indigo-800 text-sm">
ForgeHtmx uses standard HTMX attributes and behavior. It does not
introduce custom HTML attributes, wrappers, or DSLs. Learn HTMX once
and use it anywhere.
</p>
</div>
<div
class="bg-blue-50 border border-blue-200 rounded-lg p-6 mb-6"
>
<h2
class="font-semibold text-lg mb-4 text-blue-900"
>
What Forge Adds
</h2>
<div class="grid md:grid-cols-2 gap-4">
<div class="space-y-2">
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>Zero-config HTMX asset
injection</span
>
</div>
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>Automatic CSRF token
configuration</span
>
</div>
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>Conditional full/partial view
rendering</span
>
</div>
</div>
<div class="space-y-2">
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>12 HTMX response helpers</span
>
</div>
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>No JavaScript frameworks
required</span
>
</div>
<div
class="flex items-center text-blue-800"
>
<i
class="fas fa-check-circle mr-2"
></i
><span
>Middlewares that respect
HX-Request</span
>
</div>
</div>
</div>
</div>
<div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-6">
<p class="text-sm text-yellow-800">
<strong>Requirements:</strong> ForgeHtmx
depends on
<code class="bg-yellow-100 px-2 py-1 rounded">forge-router</code>
and
<code class="bg-yellow-100 px-2 py-1 rounded">forge-view</code>.
Both are included with a standard Forge
installation.
</p>
</div>
</section>
<!-- Installation -->
<section
id="installation"
class="section-anchor mb-12"
>
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Installation
</h2>
<p class="text-gray-600 mb-4">
Install ForgeHtmx via the Forge CLI:
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-bash">php forge.php package:install-module --module=forge-htmx</code></pre>
</div>
<p class="text-gray-600 mb-4">
The module auto-registers and asset symlinks are
handled automatically.
</p>
</section>
<!-- Basic Usage -->
<section id="basic-usage" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Basic Usage
</h2>
<p class="text-gray-600 mb-4">
Use any standard HTMX attribute (<code class="bg-gray-100 px-2 py-1 rounded">hx-post</code>, <code class="bg-gray-100 px-2 py-1 rounded">hx-get</code>, <code class="bg-gray-100 px-2 py-1 rounded">hx-target</code>, <code class="bg-gray-100 px-2 py-1 rounded">hx-swap</code>, etc.) on your HTML elements. ForgeHtmx doesn't replace HTMX — you get the full HTMX attribute set. The controller handles the request with <code class="bg-gray-100 px-2 py-1 rounded">htmxFragment()</code> to return the raw HTML.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><!-- View: hx-post triggers a POST to /languages, swaps result into #language-switcher -->
<button hx-post="/languages" hx-target="#language-switcher">Switch Language</button>
<div id="language-switcher"></div></code></pre>
</div>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">// Controller: returns the rendered fragment
#[Endpoint(path: "/languages", method: "POST")]
public function languages(): Response
{
return $this->htmxFragment(component(
name: 'ForgeLanguage:language-switcher',
props: [
'definition' => new LanguageSwitcherDefinition(
showFlags: true,
showLabels: true,
showCodes: false,
)
]
));
}</code></pre>
</div>
</section>
<!-- Traits -->
<section id="traits" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Traits Available
</h2>
<p class="text-gray-600 mb-4">
ForgeHtmx provides two independent traits.
</p>
<div
class="bg-gray-50 border border-gray-200 rounded-lg p-6 mb-6"
>
<h3 class="font-semibold text-lg mb-2">
HtmxResponseHelper
</h3>
<p class="text-gray-600 text-sm mb-2">
<code class="bg-gray-100 px-2 py-1 rounded"
>Modules\ForgeHtmx\Traits\HtmxResponseHelper</code
>
</p>
<p class="text-gray-600">
Standalone trait — no view dependency. Use
in any controller that needs to return HTMX
responses (redirects, triggers, location
changes, etc.).
</p>
</div>
<div
class="bg-gray-50 border border-gray-200 rounded-lg p-6"
>
<h3 class="font-semibold text-lg mb-2">
HtmxViewHelper
</h3>
<p class="text-gray-600 text-sm mb-2">
<code class="bg-gray-100 px-2 py-1 rounded"
>Modules\ForgeHtmx\Traits\HtmxViewHelper</code
>
</p>
<p class="text-gray-600">
For controllers that already use a
<code class="bg-gray-100 px-2 py-1 rounded"
>view()</code
>
method (e.g. from ForgeView). Provides
<code class="bg-gray-100 px-2 py-1 rounded"
>htmxView()</code
>
which returns a full page or a partial view
depending on whether the request includes
the
<code class="bg-gray-100 px-2 py-1 rounded"
>HX-Request</code
>
header.
</p>
</div>
</section>
<!-- Response Helpers -->
<section
id="response-helpers"
class="section-anchor mb-12"
>
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Response Helpers
</h2>
<p class="text-gray-600 mb-6">
All methods live in
<code class="bg-gray-100 px-2 py-1 rounded"
>HtmxResponseHelper</code
>
and use the
<code class="bg-gray-100 px-2 py-1 rounded"
>htmx</code
>
prefix for consistency.
</p>
<div class="overflow-x-auto mb-8">
<table
class="min-w-full divide-y divide-gray-200 border"
>
<thead class="bg-gray-50">
<tr>
<th
class="px-4 py-3 text-left text-sm font-semibold text-gray-700"
>
Method
</th>
<th
class="px-4 py-3 text-left text-sm font-semibold text-gray-700"
>
Description
</th>
</tr>
</thead>
<tbody
class="divide-y divide-gray-200 text-sm"
>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxFragment()
</td>
<td class="px-4 py-3 text-gray-600">
Return raw HTML for HTMX swap
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxRedirect()
</td>
<td class="px-4 py-3 text-gray-600">
Client-side redirect via
HX-Redirect
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxRefresh()
</td>
<td class="px-4 py-3 text-gray-600">
Force full page refresh via
HX-Refresh
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxTrigger()
</td>
<td class="px-4 py-3 text-gray-600">
Fire client-side events via
HX-Trigger
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxTriggerAfterSwap()
</td>
<td class="px-4 py-3 text-gray-600">
Fire events after swap via
HX-Trigger-After-Swap
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxTriggerAfterSettle()
</td>
<td class="px-4 py-3 text-gray-600">
Fire events after settle via
HX-Trigger-After-Settle
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxLocation()
</td>
<td class="px-4 py-3 text-gray-600">
Navigate to a new URL with
optional context via HX-Location
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxPushUrl()
</td>
<td class="px-4 py-3 text-gray-600">
Push a URL into history via
HX-Push-Url
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxReplaceUrl()
</td>
<td class="px-4 py-3 text-gray-600">
Replace current URL via
HX-Replace-Url
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxRetarget()
</td>
<td class="px-4 py-3 text-gray-600">
Override swap target via
HX-Retarget
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxReswap()
</td>
<td class="px-4 py-3 text-gray-600">
Override swap method via
HX-Reswap
</td>
</tr>
<tr>
<td
class="px-4 py-3 font-mono text-blue-700"
>
htmxStopPolling()
</td>
<td class="px-4 py-3 text-gray-600">
Stop polling via HTTP 286
status
</td>
</tr>
</tbody>
</table>
</div>
<h3 class="text-lg font-semibold mb-3">
Usage
</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
namespace App\Controllers;
use Modules\ForgeHtmx\Traits\HtmxResponseHelper;
use Modules\ForgeRouter\Http\Response;
class TodoController
{
use HtmxResponseHelper;
public function delete(string $id): Response
{
// Delete logic...
return $this->htmxTrigger('todo-deleted', ['id' => $id]);
}
}</code></pre>
</div>
</section>
<!-- HtmxView Helper -->
<section id="htmx-view" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Optional: HtmxViewHelper
</h2>
<p class="text-gray-600 mb-6">
Use this only when one endpoint needs to serve both
full page responses and HTMX partial responses.
<code class="bg-gray-100 px-2 py-1 rounded"
>htmxView($view, $data, $partial)</code
>
renders the full view on direct visits and the
partial (without layout) when it detects the
<code class="bg-gray-100 px-2 py-1 rounded">HX-Request</code>
header.
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">public function show(): Response
{
return $this->htmxView(
'dashboard/index', // full page
['stats' => $this->getStats()],
'dashboard/partials/stats' // partial for HTMX
);
}</code></pre>
</div>
</section>
<!-- Middleware -->
<section id="middleware" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Middleware
</h2>
<h3 class="text-lg font-semibold mb-3">
ForgeHtmxMiddleware
</h3>
<p class="text-gray-600 mb-4">
Registered in the
<code class="bg-gray-100 px-2 py-1 rounded"
>web</code
>
middleware group at order 2. It injects a
<code class="bg-gray-100 px-2 py-1 rounded"
><script></code
>
tag that configures HTMX to send the CSRF token
with every AJAX request. The middleware skips
injection when the request is an HTMX partial
(has
<code class="bg-gray-100 px-2 py-1 rounded"
>HX-Request</code
>
header).
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-javascript">// Injected automatically:
document.addEventListener("htmx:configRequest", function(e) {
var token = document.querySelector('meta[name="csrf-token"]');
if (token) e.detail.headers["X-CSRF-TOKEN"] = token.getAttribute("content");
});</code></pre>
</div>
<div
class="bg-blue-50 border-l-4 border-blue-400 p-4"
>
<p class="text-sm text-blue-700">
<strong>Note:</strong> This middleware only
handles CSRF configuration. The
<code class="bg-blue-100 px-2 py-1 rounded"
>htmx.min.js</code
>
script is injected separately by the module
class via the
<code class="bg-blue-100 px-2 py-1 rounded"
>#[RouterHookAttribute(RouterHookName::AFTER_REQUEST)]</code
>
lifecycle hook.
</p>
</div>
</section>
<!-- Common Examples -->
<section id="examples" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Common Examples
</h2>
<h3 class="text-lg font-semibold mb-3">
Partial Page Update
</h3>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">use Modules\ForgeHtmx\Traits\HtmxResponseHelper;
use Modules\ForgeHtmx\Traits\HtmxViewHelper;
class TodoListController
{
use HtmxResponseHelper;
use HtmxViewHelper;
protected function view(string $view, array $data = []): Response
{
// ... rendering logic
}
public function list(): Response
{
$todos = TodoRepository::all();
return $this->htmxView('todos/index', ['todos' => $todos], 'todos/partials/_list');
}
public function toggle(string $id): Response
{
TodoRepository::toggle($id);
$this->flash('status', 'Todo updated');
return $this->htmxTrigger('todo-toggled', ['id' => $id]);
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">
Client-Side Redirect
</h3>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">public function logout(): Response
{
auth()->logout();
return $this->htmxRedirect('/login');
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">
Return Raw HTML Fragment
</h3>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">public function search(string $query): Response
{
$results = SearchService::find($query);
$html = '';
foreach ($results as $item) {
$html .= '<div class="result-item">' . htmlspecialchars($item['title']) . '</div>';
}
return $this->htmxFragment($html);
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">
Stop Polling
</h3>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">public function checkStatus(): Response
{
if ($this->jobIsComplete()) {
return $this->htmxStopPolling();
}
return $this->htmxFragment('<div>Processing...</div>');
}</code></pre>
</div>
</section>
<!-- Under the Hood -->
<section
id="under-the-hood"
class="section-anchor mb-12"
>
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Under the Hood
</h2>
<h3 class="text-lg font-semibold mb-3">
Module Registration
</h3>
<p class="text-gray-600 mb-4">
<code class="bg-gray-100 px-2 py-1 rounded"
>ForgeHtmxModule</code
>
registers with
<code class="bg-gray-100 px-2 py-1 rounded"
>#[Module]</code
>
and uses the
<code class="bg-gray-100 px-2 py-1 rounded"
>InjectsAssets</code
>
trait to inject
<code class="bg-gray-100 px-2 py-1 rounded"
>htmx.min.js</code
>
into the response via
<code class="bg-gray-100 px-2 py-1 rounded"
>#[RouterHookAttribute(RouterHookName::AFTER_REQUEST)]</code
>. The asset is served from
<code class="bg-gray-100 px-2 py-1 rounded"
>/assets/modules/forge-htmx/js/htmx.min.js</code
>.
</p>
<h3 class="text-lg font-semibold mb-3">
HX-Request Detection
</h3>
<p class="text-gray-600 mb-4">
<code class="bg-gray-100 px-2 py-1 rounded"
>HtmxViewHelper::isHtmxRequest()</code
>
resolves the current
<code class="bg-gray-100 px-2 py-1 rounded"
>Request</code
>
from the Container and checks for the
<code class="bg-gray-100 px-2 py-1 rounded"
>HX-Request</code
>
header. Since it uses the Container rather than a
method parameter, controllers don't need to
change their method signatures.
</p>
<h3 class="text-lg font-semibold mb-3">
CSRF Integration
</h3>
<p class="text-gray-600 mb-4">
The default Forge
<code class="bg-gray-100 px-2 py-1 rounded"
>CsrfMiddleware</code
>
reads the
<code class="bg-gray-100 px-2 py-1 rounded"
>X-CSRF-TOKEN</code
>
header. ForgeHtmxMiddleware injects a
<code class="bg-gray-100 px-2 py-1 rounded"
>htmx:configRequest</code
>
event listener that reads the CSRF token from a
<code class="bg-gray-100 px-2 py-1 rounded"
><meta name="csrf-token"></code
>
tag and adds it as the
<code class="bg-gray-100 px-2 py-1 rounded"
>X-CSRF-TOKEN</code
>
header. This approach avoids timing issues with
deferred script loading.
</p>
<h3 class="text-lg font-semibold mb-3">
Stopping Polling
</h3>
<p class="text-gray-600 mb-4">
HTMX stops polling when it receives an HTTP 286
status. The
<code class="bg-gray-100 px-2 py-1 rounded"
>htmxStopPolling()</code
>
method returns
<code class="bg-gray-100 px-2 py-1 rounded"
>new Response('', 286)</code
>, which triggers HTMX to cease polling
automatically.
</p>
</section>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="bg-gray-900 text-white py-8 mt-16">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
<div class="flex items-center justify-center mb-4">
<span class="text-lg font-bold">Forge Kernel</span>
</div>
<p class="text-gray-400">
A toolbox for builders who like to do things their way.
</p>
</div>
</footer>
<script>
// Mobile menu toggle
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', function () {
mobileMenu.classList.toggle('hidden');
});
}
// Navigation highlighting
const navLinks = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('.section-anchor');
function highlightNavigation() {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (window.pageYOffset >= sectionTop - 200) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('bg-indigo-50', 'text-indigo-700', 'font-medium');
if (link.getAttribute('href') === '#' + current) {
link.classList.add('bg-indigo-50', 'text-indigo-700', 'font-medium');
}
});
}
window.addEventListener('scroll', highlightNavigation);
highlightNavigation();
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
</script>
</body>
</html>