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+.
Install the Package
composer require bengor-user/twig-bridge
Ensure your Laravel project uses the bengor-user/user package (this bridge is an adapter for it).
Register the Bridge
In your config/app.php, add the bridge to the aliases array under the Twig namespace:
'aliases' => [
// ...
'TwigBridge' => BenGorUser\TwigBridge\TwigBridge::class,
],
Configure Twig
In your config/twig.php, register the bridge as a Twig extension:
'extensions' => [
// ...
BenGorUser\TwigBridge\TwigBridge::class,
],
First Use Case
Pass a BenGorUser\User object to Twig and access its properties directly:
{{ user.getFullName() }} {# Outputs the user's full name #}
{{ user.getEmail() }} {# Outputs the user's email #}
User Model Integration
Extend Laravel’s Authenticatable with BenGorUser\User:
use BenGorUser\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
class UserModel extends Authenticatable implements User
{
use Notifiable;
// Implement BenGorUser\User methods (e.g., getFullName(), getEmail())
}
Passing Users to Views In your controller, pass the authenticated user to Twig:
public function showProfile()
{
return view('profile', [
'user' => auth()->user(), // UserModel instance
]);
}
Twig Filters/Functions Leverage TwigBridge to expose custom methods:
{# Display user's role #}
{{ user.getRole() }}
{# Check if user is active #}
{% if user.isActive() %}
<p>Active User</p>
{% endif %}
Dynamic Property Access
Use Twig’s attribute function for dynamic properties:
{{ attribute(user, 'getAvatarUrl') }}
UserModel implements all required BenGorUser\User methods.{{ user|cache(3600) }}).{{ user.getBio()|default('No bio available') }}
Outdated Package
Method Name Mismatches
BenGorUser\User methods are prefixed with get (e.g., getEmail()).getMethods() in a custom extension:
class CustomTwigBridge extends \BenGorUser\TwigBridge\TwigBridge
{
protected function getMethods()
{
return ['email', 'fullName']; // Maps to getEmail(), getFullName()
}
}
Namespace Collisions
TwigBridge is loaded after other extensions.Authentication Context
auth() helper. Pass the user explicitly:
{% if user %}
{{ user.getName() }}
{% endif %}
{{ dump(user) }} {# Inspect object structure #}
config/twig.php, set:
'debug' => env('APP_DEBUG', false),
TwigBridge to log undefined methods:
public function __call($method, $args)
{
if (!method_exists($this->user, $method)) {
Log::warning("Method {$method} not found on user");
}
return parent::__call($method, $args);
}
Custom User Classes
Override TwigBridge to support non-BenGorUser\User objects:
class CustomUserBridge extends TwigBridge
{
public function __construct($user, array $methods = [])
{
$this->user = $user;
$this->methods = $methods; // Define allowed methods
}
}
Add Twig Tests Test your custom bridge with PHPSpec:
vendor/bin/phpspec run -fpretty spec/BenGorUser/TwigBridge/CustomTwigBridgeSpec
Performance Optimization
Cache the getMethods() result if the user object is immutable:
private $methodsCache;
protected function getMethods()
{
if (!$this->methodsCache) {
$this->methodsCache = parent::getMethods();
}
return $this->methodsCache;
}
$this->app->bind('twig.bridge', function ($app) {
return new CustomTwigBridge(auth()->user());
});
BladeOne to embed Twig templates, but ensure the user object is passed correctly:
BladeOne::addNamespace('twig', resource_path('views/twig'));
return BladeOne::render('twig/profile.twig', ['user' => auth()->user()]);
How can I help you explore Laravel packages today?