Install the Bundle
composer require avanzu/profile-bundle
Ensure your composer.json meets the package's PHP/Symfony version constraints (PHP ≥5.3.2, Symfony 2.1–<3.0).
Enable the Bundle
Add to app/AppKernel.php:
new Avanzu\ProfileBundle\AvanzuProfileBundle(),
Create a User Entity
Extend Avanzu\ProfileBundle\Entity\User and annotate with @ORM\Entity:
use Avanzu\ProfileBundle\Entity\User as BaseUser;
class User extends BaseUser {}
Configure the Bundle
Update config.yml with your user_class and form type:
avanzu_profile:
user_class: AppBundle\Entity\User
form:
class:
user: AppBundle\Form\Type\UserType
Set Up Routing
Import routes in app/config/routing.yml:
avanzu_profile:
resource: "@AvanzuProfileBundle/Resources/config/routing.yml"
prefix: /
Configure Security
Define encoders, providers, and firewalls in security.yml:
security:
encoders:
AppBundle\Entity\User: sha512
providers:
avanzu_profile_bundle:
entity: { class: AppBundle\Entity\User, property: username }
First Use Case
Access /profile (protected by ROLE_USER) or /register to test registration/login flows.
Entity/User.php for base user traits.Resources/config/routing.yml for default routes.Controller/ for built-in actions (e.g., ProfileController, RegistrationController).Resources/views/ for Twig templates (e.g., profile/edit.html.twig).Form/Type/UserType.php to customize fields.User Registration
RegistrationController or override templates (registration/register.html.twig).UserType form.Profile Management
/profile/edit. Override ProfileController or templates:
{% extends 'AvanzuProfileBundle::profile/edit.html.twig' %}
{% block profile_fields %}
{{ form_row(form.custom_field) }}
{% endblock %}
Password Resetting
/resetting/request and /resetting/reset. Extend ResettingController or templates:
{% extends 'AvanzuProfileBundle::resetting/reset.html.twig' %}
Security Integration
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
Doctrine Events Listen to user lifecycle events (e.g., post-persist) in your bundle:
// src/AppBundle/EventListener/UserListener.php
public function postPersist(User $user)
{
$user->setLastLogin(new \DateTime());
}
Custom Fields
Add fields to UserType and entity:
// User.php
/**
* @ORM\Column(type="string")
*/
private $customField;
// UserType.php
$builder->add('customField');
API Endpoints Expose profile data via API (e.g., FOSRestBundle):
# routing.yml
app_profile_api:
path: /api/profile
defaults: { _controller: AppBundle\Controller\ProfileApiController::getProfile }
Testing
Use Symfony’s WebTestCase to test controllers:
$client->request('GET', '/profile');
$this->assertTrue($client->getResponse()->isSuccessful());
Symfony Version Mismatch
symfony/security-bundle:^2.8) or migrate to a modern alternative like FOSUserBundle.Password Encoding
sha512. For modern apps, use bcrypt or argon2:
security:
encoders:
AppBundle\Entity\User: bcrypt
symfony/security-core:^3.0 or manual encoder setup.Routing Conflicts
/profile, /register) may clash with existing routes.routing.yml prefixes or use _controller overrides.Template Overrides
AvanzuProfileBundle::profile/edit.html.twig) breaks layouts.{% extends %} and {% block %} for partial overrides.Form Validation Errors
UserType constraints and Symfony’s Validator component:
// UserType.php
$builder->add('email', EmailType::class, [
'constraints' => [new NotBlank(), new Email()]
]);
Authentication Failures
security.yml provider/class matches your user_class.php bin/console debug:security
Doctrine Migrations
User, generate migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Custom Controllers Override bundle controllers by re-routing:
# routing.yml
app_profile:
resource: "@AppBundle/Controller/ProfileController.php"
type: annotation
prefix: /profile
Event Subscribers
Hook into user events (e.g., UserEvents::POST_REGISTER):
// src/AppBundle/EventListener/RegistrationListener.php
public function onRegistration(UserEvent $event)
{
$user = $event->getUser();
$user->setActive(true);
}
API Integration
Use Symfony’s Serializer to expose user data:
// src/AppBundle/Serializer/UserNormalizer.php
public function normalize($user, $format = null, array $context = [])
{
return [
'id' => $user->getId(),
'email' => $user->getEmail(),
];
}
Custom Roles
Add roles to User entity and update security.yml:
// User.php
private $roles = ['ROLE_USER'];
// security.yml
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
avanzu/tools-bundle (dev dependency) for helper utilities like form builders.{{ user.full_name }} {# Calls a custom method #}
How can I help you explore Laravel packages today?