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

Ccdn User Bundle Laravel Package

codeconsortium/ccdn-user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Require the bundle via Composer:

    composer require codeconsortium/ccdn-user-bundle
    

    Enable the bundle in config/bundles.php:

    return [
        // ...
        CodeConsortium\CCDNUserBundle\CCDNUserBundle::class => ['all' => true],
    ];
    
  2. Database Configuration Since this is a child of FOSUserBundle, ensure your User entity extends FOS\UserBundle\Model\User (or its proxy). Run migrations:

    php bin/console doctrine:migrations:diff
    php bin/console doctrine:migrations:migrate
    
  3. First Use Case: User Registration Extend the default registration form by overriding the template:

    {% extends 'CCDNUserBundle:Registration:register.html.twig' %}
    

    Or customize the registration process in a controller:

    use Symfony\Component\HttpFoundation\Request;
    use FOS\UserBundle\Form\Type\RegistrationFormType;
    
    public function registerAction(Request $request)
    {
        $form = $this->createForm(RegistrationFormType::class);
        // ...
    }
    

Implementation Patterns

Core Workflows

  1. Authentication & Authorization Leverage FOSUserBundle’s built-in security:

    # config/packages/security.yaml
    security:
        providers:
            fos_userbundle:
                id: fos_user.user_provider.username_email
        firewalls:
            main:
                form_login:
                    provider: fos_userbundle
                    csrf_token_generator: security.csrf.token_manager
                logout: true
                anonymous: true
    
  2. Custom User Fields Extend the User entity (e.g., 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;
    
        // Getters/setters...
    }
    

    Update the registration form type:

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    class CustomRegistrationType extends RegistrationFormType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
            $builder->add('customField');
        }
    }
    
  3. Event Listeners Hook into FOSUserBundle events (e.g., redirect after login):

    use FOS\UserBundle\FOSUserEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class CustomUserSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [
                FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin',
            ];
        }
    
        public function onSecurityImplicitLogin(GetResponseUserEvent $event)
        {
            $event->setResponse($this->redirect('/dashboard'));
        }
    }
    
  4. Twig Extensions Add user-specific helpers to Twig:

    use Twig\Extension\AbstractExtension;
    use Twig\TwigFunction;
    
    class UserExtension extends AbstractExtension
    {
        public function getFunctions()
        {
            return [
                new TwigFunction('is_verified', [$this, 'isUserVerified']),
            ];
        }
    
        public function isUserVerified($user)
        {
            return $user->isEnabled();
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Symfony 2.4 Dependency

    • This bundle only supports Symfony 2.4 and PHP 5.4. Avoid using with newer Symfony versions without forking/updating.
    • Workaround: Use a compatibility layer (e.g., symfony/symfony:2.4.*) in a subdirectory if migrating.
  2. FOSUserBundle Conflicts

    • If FOSUserBundle is already installed, ensure this bundle is loaded after it in bundles.php to avoid conflicts.
    • Debugging: Clear cache after changes:
      php bin/console cache:clear
      
  3. Template Overrides

    • CCDNUserBundle templates may not exist for all FOSUserBundle routes. Fall back to FOSUserBundle’s defaults:
      {% extends 'FOSUserBundle:Registration:register.html.twig' %}
      
  4. Event Dispatcher Order

    • CCDNUserBundle’s listeners may override FOSUserBundle’s. Use stopPropagation: false in subscribers if needed:
      $event->stopPropagation();
      

Tips

  1. Debugging Authentication Enable debug mode and check the security.context service:

    php bin/console debug:container fos_user.user_manager
    
  2. Custom Validation Add constraints to the User entity:

    use Symfony\Component\Validator\Constraints as Assert;
    
    /**
     * @Assert\NotBlank(message="Custom field cannot be blank.")
     */
    private $customField;
    
  3. API Integration Use the UserManager to fetch users in APIs:

    use FOS\UserBundle\Model\UserManagerInterface;
    
    public function __construct(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }
    
    public function getUser($id)
    {
        return $this->userManager->findUserBy(['id' => $id]);
    }
    
  4. Testing Mock the UserManager in PHPUnit:

    $userManager = $this->createMock(UserManagerInterface::class);
    $userManager->expects($this->any())
                ->method('findUserBy')
                ->willReturn(new User());
    $this->container->set('fos_user.user_manager', $userManager);
    
  5. Performance

    • Use SELECT queries with DISTINCT for large user datasets:
      $users = $this->userManager->findUsers();
      $uniqueRoles = array_unique(array_map(function($user) {
          return $user->getRoles();
      }, $users));
      
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