sylius/user
Sylius User component: user and customer models plus authentication-related interfaces and utilities for Sylius-based apps. Provides reusable building blocks for user management, security integration, and account features in Symfony/Laravel-adjacent PHP projects.
Installation Add the package via Composer:
composer require sylius/user
Publish migrations and configuration (if needed):
php artisan vendor:publish --provider="Sylius\User\SyliusUserServiceProvider"
Run migrations:
php artisan migrate
Basic Usage
The package provides a User model (extendable) and a UserRepository for CRUD operations.
Example:
use Sylius\User\Model\UserInterface;
use Sylius\User\Repository\UserRepositoryInterface;
// Create a user
$user = new \Sylius\User\Model\User();
$user->setEmail('user@example.com');
$user->setPlainPassword('secure123'); // Handles hashing automatically
$userRepository->add($user);
// Find a user
$user = $userRepository->findOneBy(['email' => 'user@example.com']);
First Use Case: Authentication Integrate with Laravel’s built-in auth:
// config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \Sylius\User\Model\User::class,
],
],
Now use Auth::user() as usual.
User Management
User model with setters (e.g., setEmail(), setPlainPassword()).
Passwords are auto-hashed via UserPasswordUpdater.UserPasswordUpdater for password changes:
$updater = new \Sylius\User\Updater\UserPasswordUpdater();
$updater->update($user, 'newPassword123');
SoftDeletableTrait (if enabled).Repository Integration
UserRepositoryInterface for queries:
$user = $userRepository->findOneBy(['email' => 'test@example.com']);
$users = $userRepository->findBy(['role' => 'ROLE_ADMIN']);
UserRepository for custom logic:
class CustomUserRepository extends UserRepository {}
Events & Listeners
UserCreated, UserUpdated) via UserEvents:
event(new UserCreated($user));
$dispatcher->addListener(
UserEvents::CREATED,
function (UserCreatedEvent $event) { /* ... */ }
);
spatie/laravel-permission for RBAC:
$user->assignRole('admin');
Sylius\Component\User\Security\PasswordGenerator for secure password generation.UserRepositoryInterface for unit tests:
$this->mock(UserRepositoryInterface::class)->shouldReceive('findOneBy')->andReturn($user);
Password Hashing
setPlainPassword() and manually hashing passwords.setPlainPassword()—the package handles hashing via UserPasswordUpdater.UserPasswordUpdater for errors if passwords fail to validate.Soft Deletes
find() returns null for soft-deleted users by default.findOneBy() or enable withTrashed():
$userRepository->findOneBy(['email' => 'test@example.com'], null, null, ['trashed' => true]);
Event Dispatching
EventDispatcher binding.SyliusUserServiceProvider is registered and events are dispatched manually if needed:
$dispatcher->dispatch(new UserCreated($user));
config/logging.php for user-related events.users table (check for deleted_at if soft deletes are enabled).sylius/user-tests for test utilities (if available).Custom User Model
Extend \Sylius\User\Model\User to add fields:
class CustomUser extends User
{
protected $customField;
public function setCustomField($value): void
{
$this->customField = $value;
}
}
Update config/auth.php to point to CustomUser.
Custom Repository
Override UserRepository methods:
class CustomUserRepository extends UserRepository
{
public function findByRole(string $role): array
{
return $this->createQueryBuilder('u')
->where('u.role = :role')
->setParameter('role', $role)
->getQuery()
->getResult();
}
}
Password Rules
Customize validation in UserPasswordUpdater:
$validator = Validator::make(['password' => $plainPassword], [
'password' => ['required', 'min:12', 'regex:/[A-Z]/'], // Custom rules
]);
spatie/laravel-permission or similar.UniqueEntity validator (check User model constraints).How can I help you explore Laravel packages today?