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

dywee/user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle to your composer.json:

    composer require dywee/user-bundle
    

    Register the bundle in app/AppKernel.php:

    new Dywee\UserBundle\DyweeUserBundle(),
    
  2. Database Migration Run the bundle’s migration to create the users table:

    php bin/console doctrine:migrations:diff
    php bin/console doctrine:migrations:migrate
    

    (Note: Ensure doctrine/doctrine-bundle is installed.)

  3. First Use Case Register a new user via the CLI or a controller:

    php bin/console dywee:user:create --email=test@example.com --password=secret
    

    Or programmatically:

    use Dywee\UserBundle\Entity\User;
    $user = new User();
    $user->setEmail('test@example.com');
    $user->setPassword('secret'); // Automatically hashed
    $em->persist($user);
    $em->flush();
    

Implementation Patterns

Core Workflows

  1. User Registration Extend the User entity or use the bundle’s built-in registration form:

    // In a controller
    $form = $this->createForm(UserType::class, $user);
    if ($form->isSubmitted() && $form->isValid()) {
        $em->persist($user);
        $em->flush();
        // Redirect or flash success
    }
    
  2. Authentication Use Symfony’s security component with the bundle’s UserProvider:

    # config/security.yaml
    providers:
        dywee_user_provider:
            id: dywee_user.user_provider
    
  3. Role Management Assign roles dynamically:

    $user->addRole('ROLE_ADMIN'); // Uses Symfony's RoleInterface
    
  4. API Integration Serialize users with JMS Serializer or API Platform:

    # config/serializer/User.entity.yaml
    Dywee\UserBundle\Entity\User:
        exclusion_policy: ALL
        properties:
            id: { expose: true }
            email: { expose: true }
            roles: { expose: true }
    

Integration Tips

  • Custom Fields: Extend the User entity and update the UserType form class.
  • Events: Listen to dywee.user.created or dywee.user.updated events for post-actions.
  • Validation: Override constraints in User entity or use Symfony’s validator directly.

Gotchas and Tips

Pitfalls

  1. Password Hashing The bundle auto-hashes passwords, but ensure you’re not double-hashing (e.g., avoid password_hash() in your code).

  2. Doctrine Lifecycle Callbacks Avoid @PrePersist/@PreUpdate in the User entity for password hashing—let the bundle handle it via UserListener.

  3. Role Hierarchy Symfony’s role hierarchy (e.g., ROLE_ADMIN > ROLE_USER) must be defined in security.yaml:

    role_hierarchy:
        ROLE_ADMIN: [ROLE_USER]
    
  4. Migration Conflicts If you modify the users table schema, run:

    php bin/console doctrine:schema:update --force
    

Debugging

  • User Not Found: Verify the UserProvider is correctly configured in security.yaml.
  • Form Errors: Check if UserType is properly registered as a service:
    services:
        Dywee\UserBundle\Form\UserType:
            tags: ['form.type']
    
  • Console Command Issues: Ensure the dywee_user namespace is auto-loaded in composer.json.

Extension Points

  1. Custom User Class Override the User entity by extending Dywee\UserBundle\Entity\User and update the bundle’s configuration:

    dywee_user:
        user_class: App\Entity\CustomUser
    
  2. Email Verification Implement a UserListener to send verification emails post-registration:

    use Dywee\UserBundle\Event\UserEvents;
    $dispatcher->addListener(UserEvents::USER_CREATED, function ($event) {
        // Send email logic
    });
    
  3. API Tokens Integrate with lexik/jwt-authentication-bundle for token-based auth:

    // In User entity
    use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
    class User implements JWTUserInterface { ... }
    
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