codeconsortium/ccdn-user-profile-bundle
Installation
Add the bundle to your composer.json:
composer require codeconsortium/ccdn-user-profile-bundle
Enable it in app/AppKernel.php under new CCDNUserProfileBundle().
First Use Case
The bundle provides a UserProfile entity and related services. Start by generating the entity and its CRUD operations:
php app/console doctrine:generate:entity CCDNUserProfileBundle:UserProfile
php app/console doctrine:generate:crud CCDNUserProfileBundle:UserProfile
Access the generated CRUD routes (e.g., /user_profile/).
Configuration
Check app/config/config.yml for default settings. Override as needed:
ccdn_user_profile:
# Customize fields, validation, or services here
default_profile_fields: ['first_name', 'last_name', 'email']
Profile Management
UserProfileManager service to handle profile logic:
$manager = $this->get('ccdn_user_profile.manager');
$profile = $manager->createProfile($userId, ['first_name' => 'John']);
$manager->updateProfile($profile, ['last_name' => 'Doe']);
validation.yml).Integration with Security
User entity to include a UserProfile relationship:
// In User.php
/**
* @ORM\OneToOne(targetEntity="CCDNUserProfileBundle\Entity\UserProfile", mappedBy="user")
*/
private $profile;
UserProfileListener to auto-create profiles on user registration:
# app/config/config.yml
services:
ccdn_user_profile.listener:
class: CCDNUserProfileBundle\EventListener\UserProfileListener
tags:
- { name: kernel.event_listener, event: security.user.provider.created, method: onUserCreated }
Custom Fields
UserProfile entity:
// src/CCDN/UserProfileBundle/Entity/UserProfile.php
/**
* @ORM\Column(type="string", nullable=true)
*/
private $customField;
ccdn_user_profile:
default_profile_fields: ['first_name', 'last_name', 'custom_field']
API Exposure
# app/config/routing.yml
ccdn_user_profile_rest:
resource: "@CCDNUserProfileBundle/Resources/config/routing/rest.yml"
prefix: /api
Doctrine Migrations
UserProfile table exists. Run migrations after installation:
php app/console doctrine:schema:update --force
doctrine:migrations:diff to generate custom migrations if the bundle’s schema doesn’t fit your needs.Symfony 2.4 Legacy
php53 Docker images if needed).app/config/services.yml:
services:
ccdn_user_profile.twig.extension:
class: CCDNUserProfileBundle\Twig\ProfileExtension
tags: ['twig.extension']
Event Listeners
UserProfileListener may conflict with other bundles (e.g., FOSUserBundle). Disable it if not needed:
# app/config/config.yml
ccdn_user_profile:
auto_create_profiles: false
Validation Overrides
UserProfile entity’s validation:
// src/CCDN/UserProfileBundle/Resources/config/validation.yml
CCDN\UserProfileBundle\Entity\UserProfile:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: email
message: "Email already exists."
Check Events
security.user.provider.created) are fired:
// app/AppKernel.php
$this->registerBundles([
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new CCDN\UserProfileBundle\CCDNUserProfileBundle(),
]);
bin/console debug:event-dispatcher to list available events.Database Schema
php app/console doctrine:schema:dump-sql
Service Availability
UserProfileManager is autowired:
php app/console debug:container ccdn_user_profile.manager
Custom Profile Types
UserProfile entity for multi-tenancy or roles:
// src/AppBundle/Entity/CustomProfile.php
class CustomProfile extends \CCDN\UserProfileBundle\Entity\UserProfile
{
/**
* @ORM\Column(type="string")
*/
private $role;
}
UserProfileType form class (if needed).API Serialization
UserProfile serializer normalization:
# app/config/config.yml
jms_serializer:
metadata:
directories:
CCDNUserProfileBundle:
namespace_prefix: "CCDN\\UserProfileBundle"
path: "%kernel.root_dir%/Resources/config/serializer"
Create app/Resources/config/serializer/CCDN.UserProfileBundle.Entity.UserProfile.yml:
CCDN\UserProfileBundle\Entity\UserProfile:
exclusion_policy: ALL
properties:
id:
expose: true
firstName:
expose: true
serialized_name: first_name
Testing
// src/AppBundle/Tests/Functional/UserProfileTest.php
public function testProfileCreation()
{
$client = static::createClient();
$client->request('POST', '/user_profile', [
'first_name' => 'Test',
'last_name' => 'User',
]);
$this->assertEquals(201, $client->getResponse()->getStatusCode());
}
How can I help you explore Laravel packages today?