Software Development Web Development

How to quickly create a complete Symfony 5+ app with login and password reset

If you follow this quick guide you should be able to have a simple, yet complete Symfony 5.xx ( or 6 soon ) . There are lots and lots of baby steps. If you want to know how all of this works and much more I suggest reading this excellent article.

This is meant to be more of a cheatsheet. If you are totally new to Symfony, try this Free to read symfonycast as an introduction, or the article above.

If you want to know more about Symfony authentication read this symfonycast, some things have changed in version 5.xx.

I may eventually take the time to write Linux Shell scripts to automate all of this. The permissions are the most painful part of all of it.

I thought about setting up a simple app and saving it to a repository on github to make it even faster and easier. But the truth is the base code is moving so fast, it would be better to just create new apps with the latest Symfony version so you get the latest updates.

So step 1 is decide if you want to use pure composer or the Symfony binary. I like using plain old composer, but with the Symfony Binary you get the added benefit of a local server, security checks.

The local server setup is nice because it saves you time from having to create virtual servers. You will still need to decide how you want to run the other parts of your app like Mysql/Postgre, Redis, Memcache, RabbitMq etc. I’ve been using docker for some of these and installing others. Soon I will be experimenting with running Symfony Locally with MiniKube, which lets  you run Kubernetes locally.  I’ll have to figure that workflow out and share in another article.

Step 1 create the app

Move to the directory you want your app to exist in and type the following replacing with your project name.

 symfony new my_project_name --full

Step 2 fix permissions

Now that the app has been created you need to fix the permissions on the var folder. Change directories to your app directory. This is the one that contains bin, config, public, src, var etc. Then copy and paste this command into the command line ( ctrl + shift + v ) got to add the shift.

HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)

You must also set permissions for future files and folders in this directory.

 sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var

Almost there…

hide the pain meme
Many steps

Now you must set permissions for existing files.

sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var

I have to admit I don’t fully understand what is meant by the next permission step of setting the ”
Use the same User for the CLI and the Web Server

I think what it means, and how I got this working is, find the name of the server you are using, mine is www-data.  More info on setting the server name here and create a group for it

To find the user name your server is running as use the following command

ps -aux

Which will list out all of the processes that are running and lots of info about them. Below is an example. As you can see my nginx is running as www-data

nginx name is www-data

What I did is create a group named server-group and I added www-data to that group. I then set that group “server-group” as the group for the file. Here is a link on how to create a group in linux.

Once you have created the group and added your server user name. You now need to change group permissions on the var folder.

Make sure you are in the app directory of your app still, the one with var, src, public etc. then issue this command( change it for your name and group name)

sudo chown -R akashicseer:server-group var

Then you must make sure the new group can read and write to that directory or else you get an error. So you also need to issue this command.

sudo chmod -R 775 var

Now that all of that is out of the way. You can finally add the useful bits.

Install & Configure Doctrine

Before doing anything else you need to install and configure Doctrine as some of the other commands require it.

I like to put the database and other info in env.local instead of .env so the values are not stored in my repository. So first create .env.local For more information about the many configuration files and how they work in Symfony view this.

I prefer listing the database parts instead of a long connection string, it is easier to substitute later. I can use system variables for DATABASE_USER and DATABASE_PASSWORD.  I have also set the app environment variables like this and here too.  And you will want to see what environmental variables your symfony app is using too. I also set the configuration for things like Redis in .env.local


Now you will also need to make some changes to config/packages/doctrine.yaml

  dbname: sogi-test
  host: localhost
  port: 3306
  user: '%env(resolve:DATABASE_USER)%'
  password: '%env(resolve:DATABASE_PASSWORD)%'
  driver: pdo_mysql
  server_version: '8.0.21'
  schema_filter: ~^(?!t_)~
    # IMPORTANT: You MUST configure your server version,
    # either here or in the DATABASE_URL env var (see .env file)
    #server_version: '5.7'

Here I add the database name, user name, password (from the .env.local file) the driver etc. The Schema filter is so that doctrine doesn’t mess with my custom tables which have to start with t_ .

To learn more about Doctrine 2 with Symfony 5 I suggest this free to read symfonycast on doctrine.

Creating the User classes

For this you need to use makers. First install the maker bundle with this command.

composer require --dev symfony/maker-bundle

Now list all of the available makers with this command.

php bin/console list make

You will see one named “make:user”. Now type this command and it will ask some questions.

php bin/console make:user

After you answer the questions, it will create a user table, entity and repository class for you.

cool stuff meme
Cool stuff man.

Here I went with the default settings.

symfony make user image
How to create the users

Experiment to find out what the others do.

Create the Authentication system

I learned this the hard way by creating the registration before the authentication. This creates the login/logout forms and supporting classes. Use the following command to create the authentication system.

php bin/console make:auth

This will ask you some questions. I named everything “Loginxxx” So it would match. I first had the defaults but it was too confusing with the name mismatches.

symfony make authentication image
make:auth asks some questions

Create the registration system

Next create the registration system with this command :

php bin/console make:registration-form

This command will ask questions I mostly went with the defaults except for requiring an email to be sent to verify the email.

symfony make registration image
Questions symfony maker asks while creating the registration system

The options for redirect after registration suck because there are no other routes yet. This will be changed later.

kitten stay strong meme
See lots of baby steps

Password reset time

What good is an app if users can’t reset their passwords? Lucky for us Symfony makes this easy too, with another Maker. LOL

Before you can use this maker you must install a bundle with this command.

composer require --dev symfonycasts/reset-password-bundle

Now use this command to create the password reset classes.

 php bin/console make:reset-password

This command will ask you some questions like so

symfony reset password image
Symfony maker will ask these questions

Below where it says next it shows that you should create a migration. In order to do that next we must setup some configs.

symfony reset password image 2
Options after reset password is created

Now follow the advice in the last screenshot and do some doctrine migrations to create the needed tables for all of the Entities you now have.

You can now add more contollers and build your app. After you get another controller and some routes you can change the route to direct users to after they register. I redirect mine to the last page they viewed or to their home page. I’ll have to write an article about that too.

One reply on “How to quickly create a complete Symfony 5+ app with login and password reset”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: