User bundle for Symfony

Norbert Schvoy
2 min readSep 16, 2024

--

When I start a new project, I always have to create the same user-related stuff. It is boring, right?

In the “ancient times”, we used FOSUserBundle. I loved it, but FOSUserBundle has minimal maintenance right now. And because of the different use cases e.g.: RestAPI, standard website, etc. FOSUserBundle is not the best choice at all.

So I made a user bundle that provides a basic user entity, and repository immediately, which can be used directly after the installation.

What else can do it? Automatically hash the new password / or re-hash passwords if it is needed. You need to set the new plain password to your user entity and save it. The bundle will solve the other things.

You just need to do some easy steps and it is ready to use.

1. Install it via composer:

composer require schvoy/user-bundle

2. Create your UserRepository class and extend it:

<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\User;
use Doctrine\Persistence\ManagerRegistry;
use Schvoy\UserBundle\Repository\UserRepository as BaseUserRepository;

class UserRepository extends BaseUserRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
}

3. Create your User entity and extend it:

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Schvoy\UserBundle\Entity\User as BaseUser;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'users')]
class User extends BaseUser
{

}

Of course, you need to update your database, make a migration, etc.

4. Change the config in security.yaml:

security:
password_hashers:
App\Entity\User:
algorithm: argon2i

providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
[...]

5. Define your user class for Doctrine

doctrine:
orm:
resolve_target_entities:
Symfony\Component\Security\Core\User\UserInterface: App\Entity\User
[...]

After these steps, you can start to customize based on your preferences.

Why I don’t use MakerBundle?

I need a “ready to use” user bundle when I start a new project. With UserBundle I have Timestampable, Blameable, and Softdeletable behavior by default. MakerBundle doesn’t provide these for me.

I would like to extend the UserBundle with token handling (confirmation token, reset password token) soon, these are also not provided by MakerBundle.

NOTE: This bundle requires my another bundle https://github.com/schvoy/base-entity-bundle. You can read more about this bundle here: Base entities and behaviors for Doctrine — how I do it.

The UserBundle Github link is here: https://github.com/schvoy/user-bundle

I appreciate it if you could share with me your ideas or any contribution.

--

--