PHP API, Hooks & CLI
Htmx exposes $this->wire()->htmx (ProcessWire\Htmx) and $this->wire()->htmxCache (Totoglu\Htmx\HtmxCache), one ProcessWire hook, and three processwire-console CLI commands.
Core Service Methods ($this->wire()->htmx)
Section titled “Core Service Methods ($this->wire()->htmx)”| Method / Property | Purpose |
|---|---|
$htmx->request |
Typed Totoglu\Htmx\Request inspector (isHtmx(), isBoosted(), isPartial(), isQuery(), validateCsrf(), fake()). |
$htmx->response |
Typed Totoglu\Htmx\Response builder (redirect(), refresh(), location(), pushUrl(), retarget(), reswap(), trigger(), stopPolling(), vary()). |
$htmx->fragment |
Typed Totoglu\Htmx\Fragment renderer (addPartial(), renderPartials(), renderFile()). |
$htmx->cache |
Active Totoglu\Htmx\HtmxCache instance (also available as $this->wire()->htmxCache). |
registerComponent(string $alias, string $class) |
Adds a Component class to the explicit /hx/req allowlist. |
renderComponent(string $aliasOrClass, array $props = []) |
Mounts, authorizes, and renders a stateful Component. |
use(array|string $extensions) |
Queues one or more bundled HTMX 4 extensions for the current page. |
loadExtension(string $name) |
Loads a single bundled HTMX 4 extension by name. |
loadHyperscript() |
Enqueues the bundled Hyperscript runtime for the current page. |
handleEndpoint() |
Dispatches incoming /hx/req component requests. |
resolveRegisteredComponentClass(string $alias) |
Resolves an allowlisted component alias to its FQCN. |
resolveNormalizedEndpointPath() |
Returns the normalized endpointUrl path used in HKDF state binding. |
endpointsMatch(string $a, string $b) |
Compares two normalized endpoint paths safely. |
isAdminContext() |
Returns true when running inside the ProcessWire admin. |
getAssetConfigToken() |
Returns the deterministic cache-busting token for bundled assets. |
isSafeAssetUrl(string $url) |
Validates that a script or extension URL is safe to emit. |
enforceDemoModeGuard() |
Blocks restricted mutations when running in demo mode. |
Hookable Methods (Htmx::___*)
Section titled “Hookable Methods (Htmx::___*)”Htmx exposes one domain hookable method to control when autoExtractTargets or boosted HTMX requests intercept a ProcessWire Page::render response:
| Hook | Arguments & Return | Timing | What It Does |
|---|---|---|---|
Htmx::shouldInterceptPageRender |
(Page $page): bool |
After | Decides whether Htmx intercepts and extracts partial targets from the rendered Page response. Return false to bypass interception for binary, PDF, or export templates. |
Skip HTMX Target Extraction for PDF Templates
Section titled “Skip HTMX Target Extraction for PDF Templates”<?php
declare(strict_types=1);
namespace ProcessWire;
$wire->addHookAfter('Htmx::shouldInterceptPageRender', function (HookEvent $event): void { $page = $event->arguments(0); if ($page instanceof Page && $page->template->name === 'charter-pdf') { $event->return = false; }});CLI Commands (vendor/bin/wire)
Section titled “CLI Commands (vendor/bin/wire)”1. make:htmx:component
Section titled “1. make:htmx:component”Scaffolds a new stateful Component class in site/templates/components/ (or a custom directory):
php vendor/bin/wire make:htmx:component CharterFilterComponent --dry-run --json| Flag | Description |
|---|---|
--dir |
Custom target directory for the generated component file (defaults to componentsPath). |
--dry-run |
Preview the generated path and code without writing to disk. |
--json |
Output structured JSON result for automation. |
2. make:htmx:ui
Section titled “2. make:htmx:ui”Scaffolds a new stateless Ui primitive class in site/templates/ui/ (or a custom directory):
php vendor/bin/wire make:htmx:ui YachtCardUi --dir=site/templates/ui --dry-run --json| Flag | Description |
|---|---|
--dir |
Custom target directory for the generated Ui file (defaults to uiPath). |
--dry-run |
Preview the generated path and code without writing to disk. |
--json |
Output structured JSON result for automation. |
3. htmx:cache:clear
Section titled “3. htmx:cache:clear”Inspects, prunes, or clears HtmxCache compiled UI shapes, component schemas, and render caches:
php vendor/bin/wire htmx:cache:clear --stats --jsonphp vendor/bin/wire htmx:cache:clear --class=YachtCardUi --force --jsonphp vendor/bin/wire htmx:cache:clear --prune --days=7 --force --json| Flag | Description |
|---|---|
--class |
Limit cache clearing to a specific Ui or Component class name. |
--prune |
Remove only stale cache entries older than --days instead of clearing everything. |
--days |
Age threshold in days when used with --prune. |
--stats |
Display cache statistics (file counts, sizes, shapes) without deleting files. |
--force |
Skip interactive confirmation prompt in scripts and CI pipelines. |
--json |
Output structured JSON summary. |
Troubleshooting
Section titled “Troubleshooting”SignedStateCodec Throws a Salt Length Error
Section titled “SignedStateCodec Throws a Salt Length Error”Symptom: Exception stating $config->userAuthSalt is missing or shorter than 16 characters.
Fix: Ensure site/config.php defines a strong $config->userAuthSalt of at least 16 characters. Never rotate it casually without expecting active browser tabs to reload fresh state tokens.
403 Forbidden on Component Action
Section titled “403 Forbidden on Component Action”Symptom: Clicking a button calling /hx/req returns 403.
Fix:
- Confirm the component is registered via
$htmx->registerComponent('alias', MyComponent::class)insite/ready.php. - For mutating actions (
POST/PUT/PATCH/DELETE), verify$session->CSRF->renderInput()is inside the form. - For
GET/QUERYactions, confirm the PHP method name starts withquery(e.g.,queryFilter). - Check
authorize()andauthorizeAction($action)returntruefor the current user.
Stale Markup After Editing a Stateless Ui
Section titled “Stale Markup After Editing a Stateless Ui”Symptom: Changes to a Ui class template do not appear immediately.
Fix: Run php vendor/bin/wire htmx:cache:clear --force (or --class=YachtCardUi) to flush the compiled shape cache in site/assets/cache/Htmx/.
Localization & Full-Page Caching
Section titled “Localization & Full-Page Caching”- 6-Locale Translation Packs: Bundled CSV packs (
en,tr,de,fr,es,ru) live insite/modules/Htmx/languages/. Import the matching.csvfile under Setup -> Languages -> [Language] -> Site Translation Files. - ProCache / CDN Exclusion: Always exclude
/hx/req(and any customendpointUrl) from full-page HTML caches so HKDF-signedhx__statetokens and CSRF fields are never shared across visitors.