FOSUserBundle is certainly the most popular bundle for Symfony to manage users.

With the last major version of Symfony (4), the organization of the code of the framework is a little different. The official documentation isn't clear about the process to install this bundle.

Below, you will found the necessary steps to use FOSUserBundle. You can get all sources of this installation on repository sf4FOSUserBundle on github.


Installation is a process in 7 steps:

  1. Symfony 4 skeleton & required components installation
  2. Download FOSUserBundle using composer
  3. Create your User class
  4. Configure your application's security.yml
  5. Configure the FOSUserBundle
  6. Import FOSUserBundle routing
  7. Update your database schema

Step 1: Symfony 4 skeleton & required components installation

Symfony 4 skeleton

The first step is the installation of the symfony skeleton to start from a clean base.

composer create-project symfony/skeleton demo

All next steps must be executed in the new created directory (aka. demo)


Object-Relational-Mapper to manage database

composer require doctrine

At this step you must, as suggested by installer, modifying your .env configuration file and eventually config/packages/doctrine.yaml. In .env, the string to change is like:


You must indicate, the user, password, ip and name of the database.


Docblock Annotations Parser used with Doctrine

composer require annotations


Swiftmailer, free feature-rich PHP mailer in symfony bundle version

composer require swiftmailer-bundle


Twig, the flexible, fast, and secure template language for PHP

composer require twig

Optional : Web Server

Symfony WebServerBundle

composer require server --dev

Step 2: Download FOSUserBundle using composer

composer require friendsofsymfony/user-bundle "~2.0"

At the end of the installation you will have the following error message :

The child node "db_driver" at path "fos_user" must be configured.

Don't panic, it's normal, you must now, create your User class and configured FOSUserBundle.

Step 3: Create your User class

Create src/Entity/User.php as custom user class who extend the FOSUserBundle BaseUser class.

// src/Entity/User.php

namespace App\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

 * @ORM\Entity
 * @ORM\Table(name="fos_user")
class User extends BaseUser
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
    protected $id;

    public function __construct()
        // your own logic

Step 4: Configure your application's security.yml

Modify config/packages/security.yaml to setup FOSUserBundle security

        FOS\UserBundle\Model\UserInterface: bcrypt

        ROLE_ADMIN:       ROLE_USER

            id: fos_user.user_provider.username

            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
            pattern: ^/
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager

            logout:       true
            anonymous:    true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

Step 5: Configure the FOSUserBundle

Create a new file config/packages/fos_user.yaml for the configuration of FOSUserBundle

    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User
        address: ""
        sender_name: ""

Update config/packages/framework.yaml to add templating configuration

        engines: ['twig', 'php']

Step 6: Import FOSUserBundle routing

Create config/routes/fos_user.yaml

    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

Step 7: Update your database schema

If not already done, you must create your database

php bin/console doctrine:database:create

Update the schema with the informations from your User class entity

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

At this point, all is installed and configured to use FOSUserBundle in Symfony 4. Run the following command to check if all is ok

composer update

If you don't have any error message, you can test !

You can run the web server to test your application

php bin/console server:start

Add a comment

Previous Post