## Getting Started
### Minimal Setup
1. **Install the Bundle**
```bash
composer require bengor-user/twig-bridge-bundle
Ensure your composer.json includes symfony/twig-bundle and bengor-user/user-bundle as dependencies.
Enable the Bundle
Add to config/bundles.php:
return [
// ...
BenGorUser\TwigBridgeBundle\BenGorUserTwigBridgeBundle::class => ['all' => true],
];
First Use Case: User Data in Twig
Inject the UserBundle's user object into Twig templates by extending your base template:
{% extends 'base.html.twig' %}
{% block user_info %}
{{ app.user.username }} {# Access user properties directly #}
{{ app.user.getFullName() }} {# Call methods if available #}
{% endblock %}
Verify Configuration
Check config/packages/twig.yaml for Twig extensions and ensure UserBundle's services are autowired.
Accessing User Data
app.user in Twig templates to access the authenticated user object.{% if app.user.isAuthenticated %}
<p>Welcome, {{ app.user.email }}!</p>
{% endif %}
Custom User Properties
User entity (from UserBundle) with getters/setters for custom fields.// src/Entity/User.php
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
{{ app.user.getFullName() }}
Conditional Logic
if statements to handle guest vs. authenticated users:
{% if app.user.isAuthenticated %}
{{ include('user_dashboard.html.twig') }}
{% else %}
{{ include('login_prompt.html.twig') }}
{% endif %}
Integration with Forms
{{ form_start(form) }}
{{ form_row(form.email, {'value': app.user.email}) }}
{{ form_end(form) }}
Global Variables
config/packages/twig.yaml:
twig:
globals:
app_user: '@security.token_storage'
app_user.getToken().getUser().Custom Twig Filters/Functions
// src/Twig/AppExtension.php
class AppExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction('isAdmin', [$this, 'checkAdmin']),
];
}
public function checkAdmin($user)
{
return $user->hasRole('ROLE_ADMIN');
}
}
services.yaml:
services:
App\Twig\AppExtension:
tags: ['twig.extension']
{% if isAdmin(app.user) %}
<p>Admin Panel</p>
{% endif %}
Event Listeners for User Data
security.interactive_login to update Twig globals dynamically:
// src/EventListener/UserListener.php
class UserListener
{
public function onLogin(GetResponseUserEvent $event)
{
$user = $event->getUser();
// Update Twig globals or cache user data
}
}
services.yaml:
services:
App\EventListener\UserListener:
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
Caching User Data
// src/Service/UserCache.php
class UserCache
{
public function getCachedRoles(UserInterface $user): array
{
return $this->cache->get('user_roles_' . $user->getId(), function() use ($user) {
return $user->getRoles();
});
}
}
Outdated Dependencies
Missing UserBundle
bengor-user/user-bundle. Install it first:
composer require bengor-user/user-bundle
Class 'BenGorUser\UserBundle\UserBundle' not found → Install the missing bundle.Twig Global Overrides
app.user is undefined, verify:
UserBundle is enabled in bundles.php.TwigBridgeBundle is loaded after UserBundle and TwigBundle.security.token_storage service is properly injected.Circular Dependencies
UserBundle, TwigBridgeBundle, and custom extensions. Use lazy-loading or interfaces to decouple components.Deprecated Symfony Features
Twig_Extension instead of Twig\Extension). Update to modern equivalents if migrating to Symfony 5+:
// Old
class OldExtension extends \Twig_Extension
// New
class NewExtension extends AbstractExtension
Check Service Availability
security.token_storage service to verify the user object:
bin/console debug:container security.token_storage
user property in the output.Twig Debugging
config/packages/dev/twig.yaml:
twig:
debug: true
strict_variables: true
{{ dump(app.user) }} in templates to inspect the user object.Event Debugging
// src/EventListener/DebugListener.php
class DebugListener
{
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
$this->container->get('twig')->addGlobal('debug_user', $this->container->get('security.token_storage')->getToken()->getUser());
}
}
}
Custom User Data
User entity to add domain-specific properties/methods:
// src/Entity/User.php
public function getProfilePictureUrl(): string
{
return '/uploads/' . $this->id . '.jpg';
}
<img src="{{ app.user.getProfilePictureUrl() }}" alt="Profile">
Override Twig Globals
app.user global with a custom service:
# config/packages/twig.yaml
twig:
globals:
app_user: '@app.custom_user_service'
// src/Service/CustomUserService.php
class CustomUserService
{
public function __construct(private UserInterface $user) {}
public function getUser(): UserInterface
{
return $this->user;
}
}
Add Twig Filters
// src/Twig/UserFilter.php
class UserFilter extends AbstractExtension
{
public function getFilters()
{
return [
new FilterMethod($this, 'formatUsername', ['is_safe' => ['html']]),
];
}
public function formatUsername($username): string
{
return ucfirst(strtolower($username));
}
}
{{ app.user.username|formatUsername }}
Integration with API Platform
api-platform, expose user data via serializers:
// src/Serializer/UserNormalizer.php
class UserNormalizer implements NormalizerInterface
{
How can I help you explore Laravel packages today?