This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

Create User Management in Symfony by Integrating FOSUserBundle with EasyAdmin

Updated on December 22, 2021

4 Min Read
easyadmin

FOSUserBundle is a preferred choice to create user management(login, register, profile, logout) system in Symfony. I have already covered the following topics about FOSUserBundle:

Now imagine a scenario where you might need to manage users as a super admin and be able to modify the user information.

EasyAdmin is also known as an admin generator for Symfony applications. It makes a difference when you create the backend of your projects by solving all the tedious stuff.

Symfony has an elegant bundle called EasyAdminBundle that can manage all sorts of entities including users. This means you could manage users who engage with other entities such as products through this bundle. This bundle also solves many user management issues that you might encounter when working with the FOSUserBundle. In this article, I will highlight the integration of EasyAdminBundle with FOSUserBundle to manage and modify the registered Users.

For the purpose of this article, I am assuming that you have already installed Symfony on Cloudways, which holds a reputation as the Best PHP Hosting in the market and FOSuserbundle is ready for login and registration.

Prerequisites for Symfony User Management System

  • Symfony 3.x or 5.x
  • FOSUserBundle and EasyAdminBundle
  • Doctrine ORM entities (Doctrine ODM and Propel are not supported).

Note: Entities with composite keys or using inheritance are not supported.

Install and Enable EasyAdminBundle

The first step in the integration is to install EasyAdminBundle and enable it in the AppKernel.php file. As always, Composer is the preferred method for installation:

$ composer require javiereguiluz/easyadmin-bundle

Make sure that you have already installed Symfony and that FOSUserBundle is properly registering and login users.

Alternatively, you can also clone it from GitHub Repository.

$ git clone https://github.com/javiereguiluz/EasyAdminBundle.git

After cloning it successfully, run composer update to install the dependencies.

Now move to the AppKernel.php file and add the following line to enable the bundle.

        $bundles = [    

           new JavierEguiluz\Bundle\EasyAdminBundle\EasyAdminBundle(),

       ];

Promote the User Role To Admin

The next step is to work with the user roles for the FOS users. I just need to change a single user role to admin and make it a super user so that the admin user can manage all other registered users. If you go to your database and look at the results, you will find:

User Role

By default, all users are saved with ROLE_USER, which is translated as a:0:{}. Now, suppose that I want to upgrade the role of the user “Javier”. For this, I would use the following command:

$ php bin/console fos:user:promote Javier ROLE_ADMIN

This command will add the new admin role.

Load the Routes in routing.yml

EasyAdminBundle works at specific routes and a pre-declared easyadmin Controller. You can change the route path to access the admin panel. Copy the following code to the route.yml file:

easy_admin_bundle:

   resource: "@EasyAdminBundle/Controller/"

   type:     annotation

   prefix:   /admin

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Create Admin Controller to Modify Users

FOSUserBundle works with the `fos_user.user_manager` to perform CRUD operations on Users. This service will tell the users where the user records are saved.

I will show you how to override the `createNewUserEntity()` and `prePersistUserEntity()` methods to change the way users are created and persisted.

Create a new controller file AdminController.php in src/AppBundle/Controller and add the following code to it:

<?php

namespace AppBundle\Controller;

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

class AdminController extends BaseAdminController

{

   public function createNewUserEntity()

   {

       return $this->get('fos_user.user_manager')->createUser();

   }

   public function prePersistUserEntity($user)

   {

       $this->get('fos_user.user_manager')->updateUser($user, false);

   }

   public function preUpdateUserEntity($user)

   {

       $this->get('fos_user.user_manager')->updateUser($user, false);

   }

}

?>

The false value will abandon the FOSUserBundle to update the records and doctrine will take care of it.

Edit User Information In Config

FOSUserBundle provides the custom user entity with several defined attributes, including username and email, enabled. You also need to define these attributes in config.yml for managing in EasyAdminBundle.

easy_admin:

   entities:

       User:

           class: AppBundle\Entity\User

           list:

               fields:

                   - id

                   - username

                   - email

           form:

               fields:

                   - username

                   - email

                   - roles

                   - 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' } } }

At this point, the app is all done and ready for testing.

Demo of Symfony User Management System

When you access this URL, you will first be redirected to the FOSUser login page. Enter the username javier and the password javier. You will then be taken to the EasyAdmin user manager screen. Now, you can edit and delete the users who are registered from the FOSUserBundle.

Final Thoughts

EasyAdmin uses a number of Symfony components, which give an extraordinary and rock-solid establishment to develop the project on top of them. This includes components such as Symfony String and Symfony Uid. The Symfony String component is common among engineers because it permits you to compose super clean and clear code.

This is a very simple example of how you could easily integrate FOSUserBundle and EasyAdminBundle to create a powerful user management system. If you need help with the code or any other aspect of this Symfony user management system, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now