DEV Community

AutoIdle
AutoIdle

Posted on • Originally published at Medium on

Laravel Session configuration on Heroku

Laravel on Heroku — Tip #6

Laravel by default will save sessions into a directory on disk, which isn’t ideal, because Heroku uses an ephemeral filesystem (explained in Tip #5).

You have to use another session driver. Laravel ships with several great drivers out of the box:

  • Cookie
  • Database
  • Redis

In this example, we use Redis (personally we use always Redis, also for Cache and Queue).

  1. You have to change the default session driver to Redis in config/session.php:

or by setting the SESSION_DRIVER environment variable:

heroku config:set SESSION\_DRIVER=redis
Enter fullscreen mode Exit fullscreen mode
  1. Don’t forget to add the Redis Heroku Addon:
heroku addons:create heroku-redis:hobby-dev
Enter fullscreen mode Exit fullscreen mode
  1. Use our Tip #1 to use Heroku environment variables in your Laravel app.

That’s it, now your session will not expire again after restart or deploy 🚀

Want more tips like these?

You should follow me on Twitter! And if you’re building on Heroku, you should check out AutoIdle — the automated way to save money on your staging and review apps.

Discussion (0)