Installation
composer require bengor-user/user-bundle
Register the bundle in config/bundles.php:
return [
// ...
BenGor\UserBundle\BenGorUserBundle::class => ['all' => true],
];
Basic Configuration Follow the basic configuration guide. Key steps:
User entity (extend BenGor\UserBundle\Entity\User).config/packages/bengor_user.yaml:
bengor_user:
user_class: App\Entity\User
registration:
enabled: true
confirmation: true # Enable email confirmation
First Use Case: User Registration Use the provided controller or create a custom form:
use BenGor\UserBundle\Form\Type\RegistrationType;
$form = $this->createForm(RegistrationType::class);
Submit the form via a route (e.g., /register). The bundle handles validation, confirmation emails (if enabled), and user creation.
Multi-Type Users
Extend BenGor\UserBundle\Entity\User and implement BenGor\UserBundle\Model\UserInterface:
class Customer extends User implements UserInterface
{
// Custom fields (e.g., billing_address)
}
Configure in bengor_user.yaml:
bengor_user:
user_types:
customer: App\Entity\Customer
admin: App\Entity\Admin
Multi-Renderer Authentication
Override templates in templates/BenGorUserBundle/ (HTML) or configure JSON responses:
bengor_user:
authentication:
json_response: true # Returns JSON for API calls
JWT Authentication Enable in config:
bengor_user:
authentication:
jwt:
enabled: true
secret: '%env(JWT_SECRET)%'
Use the LoginController or create a custom endpoint to generate tokens:
use BenGor\UserBundle\Controller\LoginController;
$token = $this->get('bengor_user.jwt_token_generator')->generate($user);
Password Management
use BenGor\UserBundle\Form\Type\ChangePasswordType;
$form = $this->createForm(ChangePasswordType::class, $user);
bengor_user:
password_reset:
enabled: true
remember_password: true
Event-Driven Extensions
Listen to events (e.g., user.registered) to customize behavior:
// src/EventListener/UserListener.php
public function onUserRegistered(UserRegisteredEvent $event)
{
$user = $event->getUser();
// Send welcome email, log activity, etc.
}
Register in services.yaml:
services:
App\EventListener\UserListener:
tags:
- { name: kernel.event_listener, event: user.registered, method: onUserRegistered }
Symfony Security Integration Use the bundle’s firewall configuration:
# config/packages/security.yaml
firewalls:
main:
bengor_user:
login_path: /login
logout_path: /logout
remember_me: true
Custom Validation
Extend the User entity and add constraints:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @UniqueEntity(fields={"email"}, message="Email already taken")
*/
class User extends AbstractUser
{
// ...
}
API-First Approach For APIs, disable HTML templates and use JSON responses:
bengor_user:
templates:
enabled: false
Testing Use the bundle’s test utilities:
$user = $this->createTestUser(['role' => 'ROLE_ADMIN']);
$this->loginAs($user); // Helper method from the bundle
Outdated Package
JWT Configuration
lexik/jwt-authentication-bundle is installed and configured.Invalid token may occur if the secret in bengor_user.yaml doesn’t match the one in lexik_jwt config.Email Confirmation
config/packages/mailer.yaml.user.registration.confirmed event listeners if emails aren’t sent.User Type Mismatches
User types, ensure the user_class in config matches the entity’s namespace.Class not found may appear if the entity isn’t autoloaded.CSRF Protection
security.yaml if needed:
firewalls:
main:
csrf_protection: false
Enable Debug Mode
Set APP_DEBUG=true in .env to see detailed error messages.
Event Listener Debugging Dump events in a listener:
public function onUserRegistered(UserRegisteredEvent $event)
{
dump($event->getUser()); // Inspect user data
}
Database Schema Run migrations after installing:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Log User Actions
Extend the User entity to add created_at/updated_at timestamps:
use Gedmo\Mapping\Annotation as Gedmo;
class User extends AbstractUser
{
/**
* @Gedmo\Timestampable(on="create")
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
*/
protected $updatedAt;
}
Custom User Fields
Add fields to the User entity and update the registration form:
class User extends AbstractUser
{
private $phoneNumber;
// Getters/setters
}
Extend the form type:
class CustomRegistrationType extends RegistrationType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('phoneNumber');
}
}
Override the form class in config:
bengor_user:
registration:
form_type: App\Form\Type\CustomRegistrationType
Custom Authentication Providers
Implement BenGor\UserBundle\Security\Authentication\Provider\AuthenticationProviderInterface:
class CustomAuthProvider implements AuthenticationProviderInterface
{
public function authenticate(AuthenticationToken $token)
{
// Custom logic (e.g., OAuth)
}
}
Register in security.yaml:
security:
providers:
custom_provider:
id: App\Security\CustomAuthProvider
Override Templates
Copy templates from vendor/bengor-user/user-bundle/src/Resources/views/ to templates/BenGorUserBundle/ to customize:
registration.html.twiglogin.html.twigpassword_reset.html.twigAdd Custom Roles
Extend the User entity’s roles property or use a many-to-many relationship:
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Role")
*/
private $roles;
Update the UserInterface implementation and configure in security.yaml.
Cache User Lookups Enable Symfony’s cache for the user provider:
security:
providers:
bengor_user_provider:
entity:
class: App\Entity\User
property: email
cache: true
Batch User Operations
Use Doctrine’s BATCH_SIZE for bulk updates:
$em->createQuery('UPDATE App\Entity\User u SET u.enabled = :enabled')
->setParameter('enabled', true)
->execute(['BATCH_SIZE' => 50]);
3
How can I help you explore Laravel packages today?