Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

User Bundle Laravel Package

baconmanager/user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require baconmanager/user-bundle
    

    Ensure baconmanager/acl-bundle is also installed (required dependency).

  2. Enable the Bundle Add to config/bundles.php:

    BaconManager\UserBundle\BaconUserBundle::class => ['all' => true],
    BaconManager\AclBundle\AclBundle::class => ['all' => true],
    
  3. Database Migration Run migrations (if provided) or manually create tables based on FOSUserBundle schema (this bundle extends it). Example:

    php bin/console doctrine:migrations:diff
    php bin/console doctrine:migrations:migrate
    
  4. First Use Case Register a new user via the default FOSUserBundle registration form (extended by this bundle):

    use BaconManager\UserBundle\Entity\User;
    
    $user = new User();
    $user->setUsername('testuser');
    $user->setPlainPassword('secure123');
    $user->setEmail('test@example.com');
    $userManager = $this->get('fos_user.user_manager');
    $userManager->updateUser($user);
    

Implementation Patterns

Core Workflows

  1. User Management Extend FOSUserBundle's UserManager to leverage ACL integration:

    // In a service or controller
    $userManager = $this->get('bacon_user.user_manager');
    $user = $userManager->findUserBy(['email' => 'user@example.com']);
    
    // Assign ACL roles (if ACL-Bundle is configured)
    $aclProvider = $this->get('bacon_acl.acl_provider');
    $aclProvider->assignRole('ROLE_ADMIN', $user);
    
  2. Custom User Fields Add fields to the User entity (extend BaconManager\UserBundle\Entity\User):

    // src/Entity/CustomUser.php
    namespace App\Entity;
    
    use BaconManager\UserBundle\Entity\User as BaseUser;
    
    class CustomUser extends BaseUser
    {
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $customField;
    
        // Getters/setters...
    }
    

    Update config/packages/bacon_user.yaml to point to your custom entity.

  3. Authentication Integration Override FOSUserBundle templates or use its events (e.g., security.authentication.success):

    # config/packages/security.yaml
    services:
        App\EventListener\CustomAuthListener:
            tags:
                - { name: kernel.event_listener, event: security.authentication.success, method: onAuthenticationSuccess }
    
  4. API Integration (Symfony API Platform) Enable serialization for the user entity:

    # config/packages/api_platform.yaml
    resources:
        App\Entity\CustomUser:
            collectionOperations:
                get:
                    security: "is_granted('ROLE_USER')"
    

Integration Tips

  • ACL-Bundle Sync: Ensure baconmanager/acl-bundle is configured before using role-based features.
  • Event Subscribers: Listen to fos_user.registered or fos_user.created to trigger custom logic:
    use FOS\UserBundle\FOSUserEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class UserSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [
                FOSUserEvents::REGISTERED => 'onUserRegistered',
            ];
        }
    
        public function onUserRegistered(UserEvent $event)
        {
            // Extend user data or log registration
        }
    }
    
  • Form Customization: Override FOSUserBundle forms in config/packages/fos_user.yaml:
    registration:
        form:
            type: App\Form\RegistrationFormType
    

Gotchas and Tips

Pitfalls

  1. Missing ACL-Bundle Configuration

    • Symptom: Role assignments fail silently or throw ServiceNotFoundException.
    • Fix: Ensure baconmanager/acl-bundle is installed and configured in config/bundles.php.
  2. Entity Inheritance Issues

    • Symptom: Custom user fields are ignored or cause PropertyAccessException.
    • Fix: Clear cache and verify the User entity class is properly extended:
      php bin/console cache:clear
      
  3. Deprecated FOSUserBundle Methods

    • Symptom: UserManager methods like createUser() may not work as expected.
    • Fix: Use updateUser() or reloadUser() instead (check FOSUserBundle 2.x docs).
  4. Migration Conflicts

    • Symptom: Database migrations fail due to schema conflicts.
    • Fix: Drop existing tables or manually merge migrations:
      php bin/console doctrine:schema:drop --force
      php bin/console doctrine:schema:update --force
      

Debugging Tips

  • Enable Debug Mode: Add to config/packages/dev/doctrine.yaml:
    dbal:
        logging: true
        profiling: true
    
  • Check Events: Dump dispatched events in a subscriber:
    public function onUserRegistered(UserEvent $event)
    {
        dump($event->getUser()); // Debug user data
    }
    
  • Validate ACL Rules: Use the ACL-Bundle’s debug command:
    php bin/console bacon:acl:debug
    

Extension Points

  1. Custom User Providers Override the default UserProvider in config/packages/security.yaml:

    providers:
        bacon_user_provider:
            id: App\Security\User\CustomUserProvider
    
  2. Template Overrides Copy templates from vendor/baconmanager/user-bundle/Resources/views to templates/bacon_user/ and modify.

  3. Event Dispatching Extend existing events (e.g., fos_user.profile.edit) or create custom ones:

    // Dispatch a custom event
    $dispatcher->dispatch(new UserCustomEvent($user));
    
  4. API Resource Customization Use API Platform’s Denormalizer/Normalizer to control serialization:

    // src/Serializer/UserNormalizer.php
    use ApiPlatform\Core\Serializer\Normalizer\AbstractItemNormalizer;
    
    class UserNormalizer extends AbstractItemNormalizer
    {
        public function normalize($object, $format = null, array $context = [])
        {
            $data = parent::normalize($object, $format, $context);
            $data['custom_field'] = $object->getCustomField();
            return $data;
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware