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

awstudio-paris/user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup for Laravel (via Symfony Bridge)

Since Laravel doesn’t natively support FOSUserBundle, you’ll need to integrate it via Symfony’s components or a bridge like symfony/fos-user-bundle (note: this is the original package, not the forked awstudio-paris version). Here’s how to start:

  1. Install via Composer (use the original FOSUserBundle):

    composer require friendsofsymfony/user-bundle
    

    Note: The awstudio-paris fork appears abandoned; use the official version.

  2. Configure in config/packages/security.yaml (Symfony-style):

    security:
        providers:
            fos_userbundle:
                id: fos_user.user_provider.username_email
    
  3. Set Up Doctrine User Entity (if using ORM):

    php bin/console doctrine:phpcr:generate:fosub-model
    

    For Laravel, manually create a User model extending FOS\UserBundle\Model\User or adapt Symfony’s User class.

  4. First Use Case: Registration

    • Extend Symfony’s RegistrationController or create a Laravel controller to handle registration:
      use FOS\UserBundle\Form\Type\RegistrationFormType;
      use Symfony\Component\HttpFoundation\Request;
      
      public function register(Request $request)
      {
          $form = $this->createForm(RegistrationFormType::class);
          $form->handleRequest($request);
          if ($form->isSubmitted() && $form->isValid()) {
              $userManager = $this->get('fos_user.user_manager');
              $user = $userManager->createUser();
              $form->getData()->getUser()->setEnabled(true);
              $userManager->updateUser($user);
              return new RedirectResponse($this->generateUrl('homepage'));
          }
          return $this->render('registration.html.twig', ['form' => $form->createView()]);
      }
      

Implementation Patterns

Workflows

  1. User Lifecycle Management

    • Creation: Use UserManager to create/update users:
      $user = $userManager->createUser();
      $user->setUsername('john_doe');
      $user->setEmail('john@example.com');
      $user->setPlainPassword('secure123');
      $userManager->updateUser($user);
      
    • Activation: Handle email confirmation via UserConfirmationListener (Symfony) or manually in Laravel.
  2. Authentication Integration

    • Wire FOSUserBundle’s UserProvider with Laravel’s Auth:
      // In AuthServiceProvider (Laravel 5.5+)
      public function boot()
      {
          $this->app['auth']->provider('fos_user', function($app) {
              return new FOS\UserBundle\Model\UserProvider($app['fos_user.user_manager']);
          });
      }
      
  3. Password Reset

    • Use Symfony’s ResetPasswordController or replicate its logic:
      $tokenGenerator = $this->get('fos_user.util.token_generator');
      $token = $tokenGenerator->generateToken();
      $user->setConfirmationToken($token);
      $userManager->updateUser($user);
      // Send email with reset link (e.g., `/reset-password?token=$token`).
      
  4. Custom Fields

    • Extend the User entity:
      // src/Entity/User.php
      use FOS\UserBundle\Model\User as BaseUser;
      
      class User extends BaseUser
      {
          /**
           * @ORM\Column(type="string", length=255, nullable=true)
           */
          private $customField;
      }
      
    • Update the form type (RegistrationFormType, ProfileFormType) to include new fields.

Integration Tips

  • Laravel-Specific:
    • Use Laravel’s Hash facade for password hashing (override FOS’s UserManager if needed).
    • Replace Symfony’s Twig templates with Laravel Blade by extending controllers or using a view layer adapter.
  • ORM:
    • For Doctrine, ensure your User entity uses FOS\UserBundle\Model\UserInterface.
    • For Eloquent, create a custom UserProvider implementing Laravel’s UserProviderInterface.

Gotchas and Tips

Pitfalls

  1. Symfony vs. Laravel Mismatches

    • FOSUserBundle assumes Symfony’s Container and EventDispatcher. In Laravel:
      • Manually bind services (e.g., UserManager) to the container.
      • Use Laravel’s Event system for FOS’s listeners (e.g., RegistrationListener).
    • Fix: Create a Laravel service provider to bridge gaps:
      public function register()
      {
          $this->app->singleton('fos_user.user_manager', function($app) {
              return new UserManager($app['fos_user.model.user.class']);
          });
      }
      
  2. Email Confirmation

    • FOSUserBundle expects Symfony’s Swiftmailer. In Laravel:
      • Replace the Mailer service or mock email sending during confirmation.
    • Tip: Use Laravel’s Mail facade and override the UserConfirmationListener.
  3. Password Hashing

    • FOSUserBundle uses Symfony’s EncoderFactory. For Laravel:
      • Override the UserManager to use Hash::make():
        $user->setPassword(Hash::make($plainPassword));
        
  4. Route Conflicts

    • FOSUserBundle registers routes (e.g., /register, /login). In Laravel:
      • Exclude them from your web.php or use middleware to redirect to Laravel’s auth routes.

Debugging

  • Common Issues:

    • "Class not found": Ensure FOS\UserBundle is autoloaded (run composer dump-autoload).
    • Form validation errors: Check if your User entity implements UserInterface and AdvancedUserInterface (if using advanced features).
    • Token generation: Verify TokenGenerator is bound to the container.
  • Logging:

    • Enable Symfony’s profiler (APP_DEBUG=true) or use Laravel’s Log facade to debug FOS events.

Extension Points

  1. Custom User Classes

    • Extend FOS\UserBundle\Model\User and update fos_user.model.user.class in config.
  2. Event Subscribers

    • Listen to FOS events (e.g., GetResponseUserEvent) via Laravel’s Events:
      Event::listen('fos_user.registered', function($event) {
          // Custom logic after registration
      });
      
  3. Templates

    • Override Twig templates in templates/FOSUserBundle/ (Symfony) or convert to Blade in Laravel.
  4. Security

    • Customize firewall in config/packages/security.yaml to integrate with Laravel’s auth guards.
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui