EasyAdminBundle is THE bundle to create administration backends in Symfony.

Installation

The installation of this bundle integrating FOSUserBundle to manage users in Symfony 4 is very simple and can be do in 3 steps.

  1. Installation of FOSUserBundle
  2. Installation of EasyAdminBundle
  3. Configure the EasyAdminBundle

Installation of FOSUserBundle

Two methods to install the FOSUserBundle in Symfony 4

From scratch

You can follow all steps of my previous article How to install FOSUserBundle with Symfony 4

Cloning repository

You can clone https://github.com/vfalies/sf4FOSUserBundle on github and execute following steps :

  1. Composer installation
composer install
  1. Database connection

Edit .env file to update with good credentials the database connection

DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name
  1. Database creation

To create database

bin/console doctrine:database:create
  1. Schema creation
bin/console doctrine:schema:create
  1. Create a user
bin/console fos:user:create

Installation of EasyAdminBundle

The installation of this bundle is a simple command in a terminal:

composer require admin

Configure the EasyAdminBundle

Creating new users

Create an admin user controller src/Controller/AdminController.php to allow the user creation with easyadminbundle.

<?php
// src/AppBundle/Controller/AdminController.php
namespace App\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;

class AdminController extends BaseAdminController
{
    public function createNewUserEntity()
    {
        return $this->get('fos_user.user_manager')->createUser();
    }

    public function persistUserEntity($user)
    {
        $this->get('fos_user.user_manager')->updateUser($user, false);
        parent::persistEntity($user);
    }
}

Editing user information

Edit config/packages/easy_admin.yaml to specify the comportment of user edition

easy_admin:
    entities:
        User:
            class: App\Entity\User
            form:
                fields:
                    - username
                    - email
                    - enabled
                    - lastLogin
                    # if administrators are allowed to edit users' passwords and roles, add this:
                    - { property: 'plainPassword', type: 'text', type_options: { required: false } }
                    - { property: 'roles', type: 'choice', type_options: { multiple: true, choices: { 'ROLE_USER': 'ROLE_USER', 'ROLE_ADMIN': 'ROLE_ADMIN' } } }

Update src/Controller/AdminController.php to add the following method:

    public function updateUserEntity($user)
    {
        $this->get('fos_user.user_manager')->updateUser($user, false);
        parent::updateEntity($user);
    }

Usage

The access to admin, you can use the url /admin path on your project domain

http://yourdomain/admin

You must have create a user before the login, be careful it must have the correct role to access to the admin : ROLE_ADMIN

To create a new user, you can use console command:

bin/console fos:user:create

If the user is already created, you can promote it with the command:

bin/console fos:user:promote

Next Post Previous Post