Installation
composer require demontpx/user-bundle
Ensure your project uses Symfony 5.x/6.x (FOSUserBundle dependency).
Basic Configuration
Add demontpx_user.yaml to config/packages/:
demontpx_user:
roles:
ROLE_ADMIN: Administrator
ROLE_USER: Regular User
First Use Case
use Demontpx\UserBundle\Entity\User;
$user = new User();
$user->setUsername('testuser');
$user->setPlainPassword('testuser'); // Auto-set by bundle
$user->setRoles(['ROLE_USER']);
$entityManager->persist($user);
Testing
Use UserWebTestCase for functional tests:
use Demontpx\UserBundle\Tests\UserWebTestCase;
class MyTest extends UserWebTestCase {
public function testAdminAccess() {
$this->loadUserFromFixture('admin'); // Requires test/security.yml config
$this->assertTrue($this->client->getResponse()->isSuccessful());
}
}
Role Management
demontpx_user.yaml (e.g., ROLE_ADMIN, ROLE_GROUP_MANAGER).$user->setRoles(['ROLE_ADMIN', 'ROLE_GROUP_MANAGER']);
Fixtures for Testing
config/packages/test/demontpx_user.yml:
demontpx_user:
fixtures:
admin: { roles: [ROLE_ADMIN] }
editor: { roles: [ROLE_EDITOR], email: 'editor@example.com' }
$this->loadUserFromFixture('admin');
Authentication
# config/packages/security.yaml
security:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
main:
form_login:
provider: fos_userbundle
Frontend Integration
// assets/app.scss
@import 'user-bundle';
select2 elements (e.g., role selectors) via JavaScript:
$('.select2').select2();
Custom User Fields
Extend the User entity (inherits from FOSUserBundle’s User):
// src/Entity/User.php
namespace App\Entity;
use Demontpx\UserBundle\Entity\User as BaseUser;
class User extends BaseUser {
private $customField;
// Add getters/setters
}
Password Handling
# config/packages/test/demontpx_user.yml
demontpx_user:
fixtures:
admin: { roles: [ROLE_ADMIN], password: 'SecurePass123!' }
Firewall Configuration
UserWebTestCase requires http_basic in test/security.yml. Skip this in production or use form_login instead.Entity Inheritance
User may break bundle features if not done carefully. Prefer composition for complex extensions:
class User extends BaseUser {
private $profile;
public function __construct() {
parent::__construct();
$this->profile = new UserProfile();
}
}
Select2 Dependencies
select2 class hint assumes jQuery and Select2 are loaded. Verify assets are enqueued:
{{ encore_entry_link_tags('app') }}
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
Fixture Loading
$this->assertTrue($this->loadUserFromFixture('admin')->isLoaded());
Role Assignment
dump($user->getRoles()); // Returns array of roles
SCSS Compilation
assets/user-bundle.scss is compiled by Encore:
// webpack.config.js
Encore
.addEntry('app', './assets/app.js')
.addEntry('user-bundle', './assets/user-bundle.scss')
.enableSassLoader();
Custom User Provider
Override the default provider in security.yaml:
security:
providers:
app_user_provider:
id: App\Security\UserProvider
Event Listeners
Subscribe to FOSUserBundle events (e.g., UserRegistered):
// src/EventListener/UserListener.php
namespace App\EventListener;
use Demontpx\UserBundle\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
UserEvent::REGISTERED => 'onUserRegistered',
];
}
public function onUserRegistered(UserEvent $event) {
$user = $event->getUser();
// Custom logic (e.g., send welcome email)
}
}
API Integration
Use the User entity with API Platform or similar:
# config/api/platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
collection:
operations:
- { method: 'GET', path: '/users', controller: 'api.user_action' }
How can I help you explore Laravel packages today?