bengor-user/twig-bridge
TwigBridge is an adapter that makes BenGorUser “User” objects compatible with Twig templates. Install via Composer and use it to expose user data cleanly in Twig-based views. Requires PHP 5.5+.
User class (likely part of the BenGorUser library). It bridges a PHP object (User) with Twig’s templating engine, enabling seamless rendering of user data in views.twig/bridge and integrates with Symfony’s UserInterface. This package is redundant if using Laravel’s native authentication (e.g., Illuminate\Auth\User) but valuable if:
User model (e.g., from BenGorUser) that doesn’t natively extend Laravel’s Authenticatable.User object to expose Twig-compatible methods (e.g., __toString(), getFullName()). This is clean but limited in scope.twig/twig) and configured in Laravel (e.g., via spatie/laravel-twig).User class is already instantiated and passed to Twig templates.// In a Laravel controller
$user = auth()->user(); // or custom User instance
return Twig::render('profile.twig', ['user' => $user]); // Bridge auto-adapts
return_type_declaration, no named arguments).symfony/routing or twig/bridge).User properties.User class has specific methods (e.g., getFullName()). If the User model differs, the bridge may fail silently.User class not extendable with Laravel’s traits/interfaces?Authenticatable doesn’t support?User class exactly match the expected interface (e.g., getFullName())? If not, how will it be adapted?Twig::getEnvironment()->addFunction(new \Twig\TwigFunction('user_full_name', function ($user) {
return $user->getFullName();
}));
twig/twig + a Laravel Twig package (e.g., spatie/laravel-twig).User class isn’t Laravel-native.Authenticatable (redundant).User class matches the bridge’s expectations (e.g., getFullName(), getEmail()).composer require bengor-user/twig-bridge twig/twig spatie/laravel-twig
spatie/laravel-twig):
// app/Providers/TwigServiceProvider.php
public function boot()
{
$this->twig->addExtension(new \BenGorUser\TwigBridge\TwigBridgeExtension());
}
{{ auth()->user()->name }} with Twig {{ user.name }} (or custom methods exposed by the bridge).User class is already Twig-friendly.auth() helper integration).User class evolves.User methods:
class UserTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('user_full_name', [$this, 'getFullName']),
];
}
public function getFullName($user)
{
return $user->getFullName();
}
}
profile.twig).User data renders correctly.User objects to Twig.composer.json to avoid accidental updates.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| PHP 8.x incompatibility | Bridge methods fail silently | Fork and update, or use a custom extension. |
User class method mismatch |
Twig templates break | Extend the bridge or use a custom extension. |
| Twig dependency conflicts | Composer install fails | Use replace in composer.json to lock versions. |
| No active maintenance | Security vulnerabilities unpatched | Audit dependencies; replace if critical. |
How can I help you explore Laravel packages today?