Installation
Run composer require c975l/user-files-bundle (though note the bundle is read-only; consider migrating to c975L/UserBundle).
Ensure FOSUserBundle is installed (composer require friendsofsymfony/user-bundle).
Bundle Registration
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
return [
// ...
c975L\UserFilesBundle\c975LUserFilesBundle::class => ['all' => true],
FOS\UserBundle\FOSUserBundle::class => ['all' => true],
];
Database Configuration
Update config/packages/doctrine.yaml to include the bundle’s entity mappings (check src/Resources/config/doctrine/ for User.orm.xml).
First Use Case Run migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Access the registration form at /register (if enabled) or /admin for user management.
User Registration with Challenges
RegistrationFormType (located in src/Resources/config/validation.yml or src/Form/) to add custom validation (e.g., CAPTCHA or terms acceptance).src/Resources/views/Registration/register.html.twig) to include the challenge field.// src/Validator/Constraints/ChallengeValidator.php
use Symfony\Component\Validator\Constraint, Symfony\Component\Validator\ConstraintValidator;
class ChallengeValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if ($value !== 'expected_answer') {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
Avatar Uploads
User entity’s avatar field (type string for path, file for uploads).config/packages/vich_uploader.yaml (if using VichUploaderBundle):
vich_uploader:
db_driver: orm
mappings:
user_avatar:
uri_prefix: /uploads/avatars
upload_destination: '%kernel.project_dir%/public/uploads/avatars'
// src/Form/UserType.php
use Vich\UploaderBundle\Form\Type\VichFileType;
$builder->add('avatar', VichFileType::class, [
'required' => false,
'allow_delete' => true,
]);
Disabling Registration
registration.enabled: false in config/packages/fos_user.yaml to restrict registration to admin-only.fos_user.user_manager service to programmatically create users:
$user = $userManager->createUser();
$user->setUsername('admin');
$user->setPassword('secure_password');
$user->setEnabled(true);
$userManager->updateUser($user);
Admin Panel Integration
src/Controller/Admin/UserController.php) to customize user listings or actions.// src/Controller/Admin/UserController.php
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin/users/export', name: 'user_export')]
public function exportUsers(UserManager $userManager): Response {
$users = $userManager->findAll();
// Generate CSV/Excel response...
}
Deprecation Warning
Missing Documentation
src/Resources/views/ for base templates).src/Controller/ and src/Form/ for entry points.File Upload Quirks
avatar field assumes a specific directory structure (/uploads/avatars). Ensure:
mkdir -p public/uploads/avatars).chmod -R 775 public/uploads).php bin/console cache:clear
Registration Flow Issues
fos_user.registration.form.type in fos_user.yaml (ensure it points to a valid form class).src/Validator/Constraints/ or src/Resources/config/validation.yml.php bin/console debug:form fos_user_registration_form
Symfony 5+ Compatibility
# config/autoload.yaml
imports:
- { resource: "%kernel.project_dir%/vendor/c975l/user-files-bundle/config/*.yaml" }
Dump User Entity
Use a controller to inspect the User entity structure:
#[Route('/debug/user', name: 'debug_user')]
public function debugUser(EntityManagerInterface $em): Response {
$user = $em->getRepository(User::class)->find(1);
dump($user); // Check fields like `avatar`, `firstname`, etc.
return new Response('Done');
}
Template Overrides
Copy the base template from vendor/c975l/user-files-bundle/src/Resources/views/ to templates/bundles/c975luserfiles/ to override without modifying the bundle.
Event Listeners
Extend user lifecycle events (e.g., UserEvents::REGISTRATION_SUCCESS) in src/EventListener/:
// src/EventListener/UserRegistrationListener.php
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserRegistrationListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
];
}
public function onRegistrationSuccess(GetResponseUserEvent $event) {
// Send welcome email, log event, etc.
}
}
Custom User Fields
Add fields to the User entity (extend c975L\UserFilesBundle\Entity\User):
// src/Entity/User.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User extends BaseUser {
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $customField;
// Getters/setters...
}
Update the form type and validation accordingly.
API Integration Use Symfony’s serializer to expose user data:
# config/packages/serializer.yaml
framework:
serializer:
mapping:
paths: ['%kernel.project_dir%/config/serializer']
Create a UserNormalizer or use attributes:
#[Serializer\SerializedName('full_name')]
public function getFullName(): string {
return $this->getFirstname() . ' ' . $this->getLastname();
}
Multi-Tenant Support
Add a tenant_id field to the User entity and filter queries in repositories:
// src/Repository/UserRepository.php
public function findByTenant(int $tenantId) {
return $this->createQueryBuilder('u')
->where('u.tenantId = :tenantId')
->setParameter('tenantId', $tenantId)
->getQuery()
->getResult();
}
How can I help you explore Laravel packages today?