By default, a fresh project of Symfony stores sessions on files, specifically under the
/project/var/sessions/ directory, this is configured in the
framework.yaml file of the project:
# see https://symfony.com/doc/current/reference/configuration/framework.html framework: secret: '%env(APP_SECRET)%' #csrf_protection: true #http_method_override: true # Enables session support. Note that the session will ONLY be started if you read or write from it. # Remove or comment this section to explicitly disable session support. session: handler_id: session.handler.native_file save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
However, under certain conditions and architecture of your application, you will need to use Redis to store the sessions of your project. Fortunately, Symfony makes it quite easy to store sessions in memory with Redis using the
In this short article, I will explain to you how to easily configure Symfony 5 to use Redis to store the sessions.
In this article, we won't cover the installation and configuration of Redis itself, we'll assume that you:
- Have the Redis extension enabled and functional in your application server (PHP should support Redis).
- Have a Redis server up and running and you know the HOST URL, the PORT, and the PASSWORD if is necessary. In this case, we'll use a Redis server running on Docker in the host machine, so we can test if it works.
1. Define Redis parameters
The first thing you need to do is to create the parameters to access Redis in your .env file, which are usually 3:
- REDIS_HOST: The IP or domain of the Redis server e.g. 127.0.0.1 or localhost.
- REDIS_PORT: The port to access Redis, usually 6379
- REDIS_PASSWORD: Define the password to access your Redis server if necessary.
# project/.env ###> redis ### REDIS_HOST="127.0.0.1" REDIS_PORT=6379 REDIS_PASSWORD="" ###< redis ###
These parameters will be used and obtained from the configuration files.
2. Configure RedisSessionHandler
As next, configure the Redis service that will be injected into the
RedisSessionHandler class, this will require the credentials to access your Redis server:
# app/config/services.yaml services: Redis: # you can also use \RedisArray, \RedisCluster or \Predis\Client classes class: Redis calls: - connect: - '%env(REDIS_HOST)%' - '%env(int:REDIS_PORT)%' # If your Redis server requires a password, uncomment the following lines: # - auth: # - '%env(REDIS_PASSWORD)%' Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: arguments: - '@Redis'
If you want to define the prefix for the keys store in Redis and the TTL (time to live), you may define them as arguments of the
# app/config/services.yaml services: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: arguments: - '@Redis' # The prefix to use for the keys in order to avoid collision on the Redis server - prefix: ourcodeworld_ # The time to live in seconds. - ttl: 3600
So the keys stored on Redis will use the defined prefix:
3. Configure Session to Use RedisSessionHandler
Now you only need to specify the session handler should be the
RedisSessionHandler class of Symfony in your
# app/config/framework.yaml # see https://symfony.com/doc/current/reference/configuration/framework.html framework: session: handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
By doing this, you will be now using Redis to store sessions.
4. Checking sessions
You can start now your Symfony application that should work, as usual, however, the sessions will be stored in Redis. Login to your Symfony application (assuming that you have something that uses the session module of Symfony) and now, as we are using Redis from a docker container, we can easily access it through SSH and display the current sessions stored in Redis:
sf_ will be the default prefix if you didn't configure a custom one in step #2, so every session that the users create will be stored in Redis as expected!
Happy coding ❤️!
Become a more social person