Installation
Add the bundle to your composer.json:
composer require dywee/user-bundle
Register the bundle in app/AppKernel.php:
new Dywee\UserBundle\DyweeUserBundle(),
Database Migration
Run the bundle’s migration to create the users table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
(Note: Ensure doctrine/doctrine-bundle is installed.)
First Use Case Register a new user via the CLI or a controller:
php bin/console dywee:user:create --email=test@example.com --password=secret
Or programmatically:
use Dywee\UserBundle\Entity\User;
$user = new User();
$user->setEmail('test@example.com');
$user->setPassword('secret'); // Automatically hashed
$em->persist($user);
$em->flush();
User Registration
Extend the User entity or use the bundle’s built-in registration form:
// In a controller
$form = $this->createForm(UserType::class, $user);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($user);
$em->flush();
// Redirect or flash success
}
Authentication
Use Symfony’s security component with the bundle’s UserProvider:
# config/security.yaml
providers:
dywee_user_provider:
id: dywee_user.user_provider
Role Management Assign roles dynamically:
$user->addRole('ROLE_ADMIN'); // Uses Symfony's RoleInterface
API Integration Serialize users with JMS Serializer or API Platform:
# config/serializer/User.entity.yaml
Dywee\UserBundle\Entity\User:
exclusion_policy: ALL
properties:
id: { expose: true }
email: { expose: true }
roles: { expose: true }
User entity and update the UserType form class.dywee.user.created or dywee.user.updated events for post-actions.User entity or use Symfony’s validator directly.Password Hashing
The bundle auto-hashes passwords, but ensure you’re not double-hashing (e.g., avoid password_hash() in your code).
Doctrine Lifecycle Callbacks
Avoid @PrePersist/@PreUpdate in the User entity for password hashing—let the bundle handle it via UserListener.
Role Hierarchy
Symfony’s role hierarchy (e.g., ROLE_ADMIN > ROLE_USER) must be defined in security.yaml:
role_hierarchy:
ROLE_ADMIN: [ROLE_USER]
Migration Conflicts
If you modify the users table schema, run:
php bin/console doctrine:schema:update --force
UserProvider is correctly configured in security.yaml.UserType is properly registered as a service:
services:
Dywee\UserBundle\Form\UserType:
tags: ['form.type']
dywee_user namespace is auto-loaded in composer.json.Custom User Class
Override the User entity by extending Dywee\UserBundle\Entity\User and update the bundle’s configuration:
dywee_user:
user_class: App\Entity\CustomUser
Email Verification
Implement a UserListener to send verification emails post-registration:
use Dywee\UserBundle\Event\UserEvents;
$dispatcher->addListener(UserEvents::USER_CREATED, function ($event) {
// Send email logic
});
API Tokens
Integrate with lexik/jwt-authentication-bundle for token-based auth:
// In User entity
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
class User implements JWTUserInterface { ... }
How can I help you explore Laravel packages today?