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

bengor/user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bengor-user/user-bundle
    

    Register the bundle in config/bundles.php:

    return [
        // ...
        BenGor\UserBundle\BenGorUserBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration Follow the basic configuration guide. Key steps:

    • Define User entity (extend BenGor\UserBundle\Entity\User).
    • Configure config/packages/bengor_user.yaml:
      bengor_user:
          user_class: App\Entity\User
          registration:
              enabled: true
              confirmation: true  # Enable email confirmation
      
  3. First Use Case: User Registration Use the provided controller or create a custom form:

    use BenGor\UserBundle\Form\Type\RegistrationType;
    
    $form = $this->createForm(RegistrationType::class);
    

    Submit the form via a route (e.g., /register). The bundle handles validation, confirmation emails (if enabled), and user creation.


Implementation Patterns

Core Workflows

  1. Multi-Type Users Extend BenGor\UserBundle\Entity\User and implement BenGor\UserBundle\Model\UserInterface:

    class Customer extends User implements UserInterface
    {
        // Custom fields (e.g., billing_address)
    }
    

    Configure in bengor_user.yaml:

    bengor_user:
        user_types:
            customer: App\Entity\Customer
            admin: App\Entity\Admin
    
  2. Multi-Renderer Authentication Override templates in templates/BenGorUserBundle/ (HTML) or configure JSON responses:

    bengor_user:
        authentication:
            json_response: true  # Returns JSON for API calls
    
  3. JWT Authentication Enable in config:

    bengor_user:
        authentication:
            jwt:
                enabled: true
                secret: '%env(JWT_SECRET)%'
    

    Use the LoginController or create a custom endpoint to generate tokens:

    use BenGor\UserBundle\Controller\LoginController;
    
    $token = $this->get('bengor_user.jwt_token_generator')->generate($user);
    
  4. Password Management

    • Change Password (with/without old password):
      use BenGor\UserBundle\Form\Type\ChangePasswordType;
      $form = $this->createForm(ChangePasswordType::class, $user);
      
    • Forgot Password (with "remember password" flow):
      bengor_user:
          password_reset:
              enabled: true
              remember_password: true
      
  5. Event-Driven Extensions Listen to events (e.g., user.registered) to customize behavior:

    // src/EventListener/UserListener.php
    public function onUserRegistered(UserRegisteredEvent $event)
    {
        $user = $event->getUser();
        // Send welcome email, log activity, etc.
    }
    

    Register in services.yaml:

    services:
        App\EventListener\UserListener:
            tags:
                - { name: kernel.event_listener, event: user.registered, method: onUserRegistered }
    

Integration Tips

  1. Symfony Security Integration Use the bundle’s firewall configuration:

    # config/packages/security.yaml
    firewalls:
        main:
            bengor_user:
                login_path: /login
                logout_path: /logout
                remember_me: true
    
  2. Custom Validation Extend the User entity and add constraints:

    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    
    /**
     * @UniqueEntity(fields={"email"}, message="Email already taken")
     */
    class User extends AbstractUser
    {
        // ...
    }
    
  3. API-First Approach For APIs, disable HTML templates and use JSON responses:

    bengor_user:
        templates:
            enabled: false
    
  4. Testing Use the bundle’s test utilities:

    $user = $this->createTestUser(['role' => 'ROLE_ADMIN']);
    $this->loginAs($user); // Helper method from the bundle
    

Gotchas and Tips

Pitfalls

  1. Outdated Package

    • Last release: 2017. Verify compatibility with your Symfony/Laravel (if using via Bridge) version.
    • Workaround: Fork the repo and update dependencies (e.g., Symfony components) manually.
  2. JWT Configuration

    • If using JWT, ensure lexik/jwt-authentication-bundle is installed and configured.
    • Error: Invalid token may occur if the secret in bengor_user.yaml doesn’t match the one in lexik_jwt config.
  3. Email Confirmation

    • Requires a mail transport (e.g., Symfony Mailer). Configure in config/packages/mailer.yaml.
    • Debug: Check user.registration.confirmed event listeners if emails aren’t sent.
  4. User Type Mismatches

    • If extending User types, ensure the user_class in config matches the entity’s namespace.
    • Error: Class not found may appear if the entity isn’t autoloaded.
  5. CSRF Protection

    • The bundle enables CSRF for forms by default. Disable in security.yaml if needed:
      firewalls:
          main:
              csrf_protection: false
      

Debugging Tips

  1. Enable Debug Mode Set APP_DEBUG=true in .env to see detailed error messages.

  2. Event Listener Debugging Dump events in a listener:

    public function onUserRegistered(UserRegisteredEvent $event)
    {
        dump($event->getUser()); // Inspect user data
    }
    
  3. Database Schema Run migrations after installing:

    php bin/console doctrine:migrations:diff
    php bin/console doctrine:migrations:migrate
    
  4. Log User Actions Extend the User entity to add created_at/updated_at timestamps:

    use Gedmo\Mapping\Annotation as Gedmo;
    
    class User extends AbstractUser
    {
        /**
         * @Gedmo\Timestampable(on="create")
         */
        protected $createdAt;
    
        /**
         * @Gedmo\Timestampable(on="update")
         */
        protected $updatedAt;
    }
    

Extension Points

  1. Custom User Fields Add fields to the User entity and update the registration form:

    class User extends AbstractUser
    {
        private $phoneNumber;
    
        // Getters/setters
    }
    

    Extend the form type:

    class CustomRegistrationType extends RegistrationType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('phoneNumber');
        }
    }
    

    Override the form class in config:

    bengor_user:
        registration:
            form_type: App\Form\Type\CustomRegistrationType
    
  2. Custom Authentication Providers Implement BenGor\UserBundle\Security\Authentication\Provider\AuthenticationProviderInterface:

    class CustomAuthProvider implements AuthenticationProviderInterface
    {
        public function authenticate(AuthenticationToken $token)
        {
            // Custom logic (e.g., OAuth)
        }
    }
    

    Register in security.yaml:

    security:
        providers:
            custom_provider:
                id: App\Security\CustomAuthProvider
    
  3. Override Templates Copy templates from vendor/bengor-user/user-bundle/src/Resources/views/ to templates/BenGorUserBundle/ to customize:

    • registration.html.twig
    • login.html.twig
    • password_reset.html.twig
  4. Add Custom Roles Extend the User entity’s roles property or use a many-to-many relationship:

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Role")
     */
    private $roles;
    

    Update the UserInterface implementation and configure in security.yaml.


Performance Tips

  1. Cache User Lookups Enable Symfony’s cache for the user provider:

    security:
        providers:
            bengor_user_provider:
                entity:
                    class: App\Entity\User
                    property: email
                    cache: true
    
  2. Batch User Operations Use Doctrine’s BATCH_SIZE for bulk updates:

    $em->createQuery('UPDATE App\Entity\User u SET u.enabled = :enabled')
        ->setParameter('enabled', true)
        ->execute(['BATCH_SIZE' => 50]);
    

3

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.
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui