Create User Management in Symfony by Integrating FOSUserBundle with EasyAdmin

by Shahroze Nawaz  July 11, 2017

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.

symfony user management

Symfony has the elegant bundle called EasyAdminBundle that could 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 and FOSuserbundle is ready for login and registration.

Prerequisites for Symfony User Management System

  • Symfony 2.3+ or 3.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.

Related: How To Upgrade From Symfony 2.X To 3.X

Install and Enable EasyAdminBundle

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

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 AppKernel.php file and add the following line to enable the bundle.

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:

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:

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:

Create Admin Controller to Modify Users

FOSUserBundle works with the fos_user.user_manager to perform CRUD operation 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:

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, email, enabled. You also need to define these attributes in config.yml for managing in EasyAdminBundle.

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

A Live Demo of Symfony User Management System

The following is the URL of a live demo of the user management system with several users  and an admin user:

http://phpstack-21306-71265-234521.cloudwaysapps.com/fos/web/admin

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

Final Thoughts

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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...