Laravel Heroku Config Parser
, (*1)
Parse Heroku config vars like DATABASE_URL or REDIS_URL to work with Laravel., (*2)
Important
Due to a breaking change in Laravel 5.8 (thanks https://github.com/mathieutu for the feedback), you can use this method to achieve the same goal:, (*3)
https://github.com/itsDamien/laravel-heroku-config-parser/issues/2#issuecomment-469709141, (*4)
Breaking change:
https://github.com/laravel/framework/issues/27695, (*5)
https://github.com/laravel/docs/pull/5039, (*6)
Why
When adding a database or a redis server to your Heroku app, Heroku add a URL config var like this:
DATABASE_URL=postgres://usr:pwd@localhost:5432/hellodb, (*7)
Unfortunately, Laravel can't read this var, so you probably parsed it manually like this:, (*8)
heroku config:set DB_CONNECTION=pgsql
heroku config:set DB_HOST=localhost
heroku config:set DB_PORT=5432
heroku config:set DB_DATABASE=hellodb
heroku config:set DB_USERNAME=usr
heroku config:set DB_PASSWORD=pwd
Laravel Heroku Config Parser parse automatically your DATABASE_URL and REDIS_URL to dynamically set all vars needed by Laravel (see the list)., (*9)
Installation
Installation using composer:, (*10)
composer require itsdamien/laravel-heroku-config-parser
Heroku
Add these config vars:, (*11)
heroku config:set KEY_DATABASE=DATABASE_URL
heroku config:set KEY_REDIS=REDIS_URL
Laravel
Add this block code to the top of your config/database.php:, (*12)
if (class_exists('\ItsDamien\Heroku\Config\Parse')) {
new \ItsDamien\Heroku\Config\Parse();
}
Enjoy !, (*13)
ENV vars created
| DATABASE_URL |
postgres://usr:pwd@ec2-s1:5432/db1 |
mysql://usr:pwd@ec2-s2:3306/db2 |
| DB_CONNECTION |
pgsql |
mysql |
| DB_HOST |
ec2-s1 |
ec2-s2 |
| DB_PORT |
5432 |
3306 |
| DB_DATABASE |
db1 |
db2 |
| DB_USERNAME |
usr |
usr |
| DB_PASSWORD |
pwd |
pwd |
| REDIS_URL |
redis://h:pwd@ec2-s1:11469 |
| REDIS_HOST |
ec2-s1 |
| REDIS_PORT |
11469 |
| REDIS_PASSWORD |
pwd |
Customize the config var who will be parsed
You can select wich config var will be parsed by setting KEY_DATABASE and KEY_REDIS like this:, (*14)
heroku config:set KEY_DATABASE=HEROKU_POSTGRESQL_BRONZE
heroku config:set KEY_REDIS=REDIS_URL_BACKUP
License
Laravel Heroku Config Parser is open-sourced software licensed under the MIT license, (*15)