aura/html
HTML escapers and helpers for any PHP template/view layer, including tag builders and form input helpers. Easy to use via a HelperLocatorFactory; built-in helpers escape values appropriately and you can register custom helpers via the HelperLocator.
Installation:
composer require aura/html
No additional configuration is required—just autoload via Composer.
First Use Case: Escape dynamic content to prevent XSS:
use Aura\Html\HtmlHelper;
$helper = new HtmlHelper();
$safeHtml = $helper->escapeHtml('<script>alert("XSS")</script>');
// Output: <script>alert("XSS")</script>
Where to Look First:
Aura\Html\HtmlHelper (escaping), Aura\Html\FormHelper (form inputs).tests/ directory for usage examples.$helper = new HtmlHelper();
$userComment = $request->input('comment');
echo $helper->escapeHtml($userComment); // Safe for HTML context
HtmlHelper to a service container:
$this->app->singleton(HtmlHelper::class, fn() => new HtmlHelper());
$formHelper = new FormHelper();
echo $formHelper->text('username', $user->username, ['class' => 'form-control']);
// Output: <input type="text" name="username" value="john_doe" class="form-control">
text(), password(), email(), textarea(), select(), etc.hidden(), checkbox(), radio() for hidden/boolean fields.// app/Providers/AppServiceProvider.php
public function boot()
{
view()->share('html', new HtmlHelper());
}
{{ $html->escapeHtml($dynamicData) }}
HtmlHelper for context-specific escaping (e.g., HTML vs. JS vs. URL).$helper = new HtmlHelper();
$jsSafe = $helper->escapeJs('alert("Hello")'); // Escapes for JS context
$comments = Comment::all()->pluck('body');
$safeComments = array_map([$helper, 'escapeHtml'], $comments);
Double Escaping:
{{ }}), you’ll break HTML.HtmlHelper only for raw dynamic data, not Blade’s auto-escaped output.Context Mismatches:
escapeHtml() is for HTML contexts only. Use escapeJs(), escapeUrl(), or escapeAttr() for other contexts.// ❌ Wrong (breaks JS)
$helper->escapeHtml('<script>var x = "test"</script>');
// ✅ Correct
$helper->escapeJs('<script>var x = "test"</script>');
Form Helper Quirks:
FormHelper escapes values by default. To disable (e.g., for rich text):
$formHelper->text('content', $post->content, [], false);
Verify Escaping:
htmlspecialchars() as a sanity check:
assert($helper->escapeHtml('<b>test</b>') === htmlspecialchars('<b>test</b>'));
Check for False Positives:
<div>) is escaped, ensure you’re using the correct context method.Performance:
HtmlHelper is lightweight, but batch operations (e.g., array_map) on large datasets may benefit from caching escaped values.Custom Escapers:
Aura\Html\EscaperInterface for domain-specific escaping (e.g., Markdown to HTML).Form Helper Extensions:
Aura\Html\FormHelper to add custom input types (e.g., date(), color()).Integration with Laravel Validators:
Illuminate\Validation to escape sanitized input:
$validated = $request->validate(['comment' => 'required|string']);
echo $helper->escapeHtml($validated['comment']);
aura/html is zero-config. All behavior is controlled via method calls.php:8.4 in Dockerfile or .php-version if needed).Compose Helpers:
Aura\Sql for DB-driven forms):
$formHelper->select('user_id', $user->id, $users->pluck('id', 'name'));
Laravel Service Provider:
$this->app->bind(HtmlHelper::class, fn() => new HtmlHelper());
$this->app->bind(FormHelper::class, fn() => new FormHelper());
Testing:
HtmlHelper in unit tests to verify escaping:
$helper = $this->createMock(HtmlHelper::class);
$helper->method('escapeHtml')->willReturn('<b>safe</b>');
How can I help you explore Laravel packages today?