Installation
Add the bundle to your composer.json:
composer require core2pl/user-bundle
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
Core2pl\UserBundle\Core2plUserBundle::class => ['all' => true],
];
Configuration Publish the default configuration:
php bin/console core2pl:user:install
This creates a User entity (if not already present) and updates the database schema.
First Use Case Use the bundle’s built-in commands to manage users:
# Create a new user
php bin/console core2pl:user:create --username=test [email protected] --password=password
# List users
php bin/console core2pl:user:list
Authentication
The bundle integrates with Symfony’s security system. Extend the User entity to add custom fields:
// src/Entity/User.php
namespace App\Entity;
use Core2pl\UserBundle\Entity\User as BaseUser;
class User extends BaseUser
{
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $customField;
}
Update the security configuration (config/packages/security.yaml):
providers:
app_user_provider:
entity:
class: App\Entity\User
property: username
Role Management Assign roles via the CLI or programmatically:
php bin/console core2pl:user:role --username=test --role=ROLE_ADMIN
Or in code:
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['username' => 'test']);
$user->addRole('ROLE_ADMIN');
$this->getDoctrine()->getManager()->flush();
Event Listeners
Subscribe to user events (e.g., UserEvents::POST_CREATE):
// src/EventListener/UserListener.php
namespace App\EventListener;
use Core2pl\UserBundle\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
UserEvent::POST_CREATE => 'onUserCreated',
];
}
public function onUserCreated(UserEvent $event)
{
// Custom logic (e.g., send welcome email)
}
}
API Integration Use the bundle’s serializers to expose user data in APIs:
# config/packages/serializer.yaml
attributes:
Core2pl\UserBundle\Entity\User:
attributes:
password: false # Hide sensitive fields
Database Schema Conflicts
User entity, ensure the new fields are added before running migrations. The bundle’s install command may overwrite existing schema changes.diff-schema tool.Security Misconfigurations
UserPasswordHasher:
// src/Entity/User.php
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
class User extends BaseUser implements PasswordAuthenticatedUserInterface, PasswordUpgraderInterface
{
// ...
}
security.user_password_hasher in config/packages/security.yaml.CLI Command Conflicts
core2pl:user:create) may conflict with custom commands named similarly.AppKernel.Translation Issues
translations/ directory includes the bundle’s messages:
mkdir -p translations/core2pl.user.en.yaml
config/packages/translation.yaml:
paths: ['%kernel.project_dir%/translations', '%kernel.project_dir%/vendor/core2pl/user-bundle/Resources/translations']
Event Dispatching
php bin/console debug:event-dispatcher
EventDispatcher::addListener() for debugging:
$dispatcher->addListener(UserEvent::POST_CREATE, function ($event) {
error_log('User created: ' . $event->getUser()->getUsername());
});
Doctrine Queries
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Custom User Fields
User entity and extend the UserType form:
// src/Form/Extension/UserTypeExtension.php
namespace App\Form\Extension;
use Core2pl\UserBundle\Form\UserType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
class UserTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('customField');
}
public static function getExtendedType(): string
{
return UserType::class;
}
}
Authentication Providers
# config/packages/security.yaml
security:
providers:
oauth_user_provider:
id: App\Security\OAuthUserProvider
API Resources
// src/Entity/User.php
use ApiPlatform\Core\Annotation\ApiResource;
#[ApiResource]
class User extends BaseUser { ... }
How can I help you explore Laravel packages today?