Since this is a Symfony bundle, Laravel developers must use Symfony’s bridge packages (symfony/flex, symfony/console, etc.) or integrate it via Laravel’s Symfony integration (e.g., spatie/symfony-laravel). Start here:
Install via Composer (in a Laravel project with Symfony components):
composer require friendsofsymfony/user-bundle
Note: Requires symfony/security-bundle and doctrine/orm (or ODM).
Enable the Bundle (in config/bundles.php for Symfony/Laravel hybrid projects):
return [
// ...
FriendsOfSymfony\UserBundle\FOSUserBundle::class => ['all' => true],
];
Configure Database & User Model (adapt to Laravel’s config/auth.php):
# config/packages/fos_user.yaml
fos_user:
db_driver: orm # or 'mongodb', 'couchdb'
firewall_name: main
user_class: App\Entity\User # Extend FOSUserBundle’s base User class
Create a User Entity (extend BaseUser):
// src/Entity/User.php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User extends BaseUser { /* ... */ }
Run Migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Registration Flow
RegistrationController) or override them.fos_user_registration_register: /register
fos_user_registration_check_email: /check-email
FOSUserBundle provides a UserProvider for Symfony’s SecurityBundle. In Laravel:
spatie/laravel-permission or Symfony’s UserChecker for role checks.AuthenticatesUsers trait to use FOS’s UserManager:
use FOS\UserBundle\Model\UserManagerInterface;
class LoginController extends Controller {
public function __construct(private UserManagerInterface $userManager) {}
public function login(Request $request) {
$user = $this->userManager->findUserBy(['email' => $request->email]);
// Custom auth logic...
}
}
Extend the User entity and update FOS’s form types:
// src/Entity/User.php
#[ORM\Column(type: 'string', length: 255)]
private $phoneNumber;
// src/Form/Type/RegistrationFormType.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class RegistrationFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('phoneNumber', TextType::class);
}
}
Leverage FOS’s built-in reset logic:
UserManager::resetPassword().FOSUserEvents::RESET_PASSWORD_SUCCESS event listener.use FOS\UserBundle\Event\FilterUserResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomResetSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
FOSUserEvents::RESET_PASSWORD_SUCCESS => 'onResetSuccess',
];
}
public function onResetSuccess(FilterUserResponseEvent $event) {
// Send Laravel Notifications or custom logic
}
}
For Laravel APIs:
UserManager to validate tokens (e.g., JWT).$user = $this->userManager->findUserBy(['api_token' => $request->bearerToken()]);
WebTestCase or Laravel’s HttpTests with FOS’s fixtures:
public function testRegistration() {
$client = static::createClient();
$crawler = $client->request('GET', '/register');
// Assert form fields...
}
Laravel-Symfony Mismatch:
EventDispatcher and Container. Use Laravel’s service container bridges:
$this->container->get('fos_user.user_manager');
AppServiceProvider:
$this->app->bind('fos_user.user_manager', function ($app) {
return $app->make('fos_user.user_manager.default');
});
Doctrine vs. Eloquent:
illuminate/database + spatie/laravel-doctrine-orm (hybrid approach).UserManager to use Eloquent’s Model instead of Doctrine’s EntityManager.Event Dispatching:
Events and Symfony’s EventDispatcher are separate. Subscribe to FOS events via:
$dispatcher = $this->container->get('event_dispatcher');
$dispatcher->addListener(FOSUserEvents::REGISTRATION_SUCCESS, function ($event) {
// Laravel logic (e.g., send welcome email)
});
Password Hashing:
PasswordHasherInterface. For Laravel’s Hash facade:
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
$hasher = $this->container->get('security.password_hasher');
$hashed = $hasher->hashPassword($user, $plainPassword);
CSRF Protection:
csrf_token. In Laravel, use:
{{ csrf_token() }} // Laravel’s built-in CSRF
Enable FOS Debugging:
fos_user:
debug: true # Logs events to Symfony’s profiler
Common Errors:
user_class in fos_user.yaml matches your App\Entity\User./register). Use Laravel’s route aliases:
Route::get('/register', [RegistrationController::class, 'register'])->name('fos_user_registration_register');
Event Debugging:
public function onRegistration(FormEvent $event) {
dump($event->getForm()->getData());
}
Custom User Interface:
templates/FOSUserBundle/.SymfonyBridge to render Symfony templates:
use Symfony\Bridge\Twig\Extension\RoutingExtension;
$router = $this->container->get('router');
Multi-Tenant Users:
User entity with tenant_id and filter queries in UserManager:
public function findUserBy(array $criteria) {
$criteria['tenant_id'] = auth()->id();
return parent::findUserBy($criteria);
}
Social Logins:
UserProvider with Laravel Socialite:
$user = $this->userManager->createUser([
'username' => $socialUser->getId(),
'email' => $socialUser->getEmail(),
]);
Performance:
UserManager in Laravel’s cache:
$userManager = Cache::remember('fos_user_manager', 3600, function () {
return $this->container->get('fos_user.user_manager');
});
How can I help you explore Laravel packages today?