Adding user management to your Symfony application

The full experience in under 5 minutes. Maybe less.

Many applications need it and it’s super boring to setup. What if we can get us a default skeleton application with everything on board? By avoiding any boring stuff we can focus on actually business value right away!

In this post we’re going to make a Symfony application with basic user management. Yes, the whole shebang from registration to logging to forgetting and resetting your password.

Creating the application

Using Symfony Flex. It’s awesome and super fast 😃

It installs the project and basic dependencies we need. Most of them are self explanatory, but simple-bus/symfony-bridge stands out a bit.

It’s a message bus and soon can be replaced with Symfony’s core Messenger component: https://symfony.com/blog/new-in-symfony-4-1-messenger-component

Remember to enable the bundle manually as it has no Flex recipe.

Test the application

If everything went fine we now have a basic application. We can test it by starting a webserver.

$ bin/console server:start
[OK] Server listening on http://127.0.0.1:8000
# using Docker it might be
$ bin/console server:start 0.0.0.0

If we visit the URL in our browser we should see:

We don’t have a homepage configured, but feel free to do so. However for the demo we really don’t need to visit /.

Setting up user management

Our next dependency is a message driven User Bundle. It provides the basic User domain layer as well as the actual messages to operate it.

composer require msgphp/user-bundle
# make sure to execute its recipe

And now the power of a new Symfony Concept; it’s Maker Bundle. The User Bundle provides a new make command which guides you through the initial setup. It has sensible defaults and generates a working* setup.

*) Assuming all dependencies above are installed

bin/console make:user

Next we create the database schema:

# edit DATABASE_URL in .env
bin/console doctrine:database:create
bin/console doctrine:schema:update --force

It’s done.

Get the password reset token using:

bin/console doctrine:query:dql "SELECT u.passwordResetToken FROM App:User\\User u WHERE u.credential.email = 'user@domain.local'"

Prefer the Command-Line-Interface?

You can checkout the generated source code. It’s bare, minimal, and yours. Create an awesome app with it.

That’s it! Cheers and thanks for reading.