Deploying to Heroku and using Heroku for env vars

39 views
Skip to first unread message

Peter Hecht

unread,
Apr 4, 2018, 1:58:16 PM4/4/18
to Fat-Free Framework
Hello,

I hope this day finds you well.

I'm a rusty programmer in PHP.  I have a working application I have deployed to Heroku using PostgreSQL instead of MySQL.  It was relatively easy to make the switch,  Just a few lines in my config.ini file.  

Now I have the DB name and password out in a github repo,  It seems it would be much safer to set the Heroku variables as config variables and read them from F3.  

Instead of this in my config/config.ini file:
db_name=123456

I would like to do something like this:
heroku config:set DB_NAME=123456

and then somewhere in F3 read the environment variable.

db_name=getenv('DB_NAME')

My question is, where would I put that?  After the require call: $f3=require('lib/base.php');
Some kind of different $f3->config call? 

Thanks!!!

ved

unread,
Apr 4, 2018, 2:21:51 PM4/4/18
to Fat-Free Framework
Hi, 
 
My question is, where would I put that?  After the require call: $f3=require('lib/base.php');

Sure, that should work. 
Or maybe it may be better to do it right before you initialize your database connection.
 
Some kind of different $f3->config call? 

To still use F3's ini to set it up, you can maybe set something like this on your ini:

[heroku]
host
=DB_HOST
name
=DB_NAME
user
=DB_USER
pass=DB_PASS

Then, when initializing your database, use
getenv($f3->get('heroku.name'))

instead of
$f3->get('db_name')

This is probably a bit redundant, but allows you to just change the ENV names on your ini file instead of having to edit php files if you ever change them.

Good luck.
 

 

Richard Goldstein

unread,
Apr 4, 2018, 2:40:22 PM4/4/18
to ved via Fat-Free Framework, Fat-Free Framework
I usually combine this with dotenv. So I don’t have to pollute my local environment.

See https://packagist.org/packages/vlucas/phpdotenv

I use the presence or absence of one of the environment variables as a flag to determine if the .env file should be loaded or not.
--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/8a22dd89-567c-48cc-8930-1c8d4c191bce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Rich Goldstein, MD

ved

unread,
Apr 4, 2018, 3:10:26 PM4/4/18
to Fat-Free Framework
Sure, but we don't take too kindly to non fat-free things around here :)

Richard Goldstein

unread,
Apr 4, 2018, 4:27:45 PM4/4/18
to ved via Fat-Free Framework, Fat-Free Framework
That’s just silly. 😀
--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.

For more options, visit https://groups.google.com/d/optout.

Peter Hecht

unread,
Apr 13, 2018, 4:59:45 PM4/13/18
to Fat-Free Framework
Hello,

Kind of answered it myself.  Just to document it:
if ( file_exists( '.env2' ) ) {
    $f3->config( '.env2' );
} else {
    $temp = getenv( 'db_dns' );
    $f3->set( 'db_dns', $temp );
    $temp = getenv( 'db_name' );
    $f3->set( 'db_name', $temp );
    $temp = getenv( 'db_pass' );
    $f3->set( 'db_pass', $temp );
    $temp = getenv( 'db_user' );
    $f3->set( 'db_user', $temp );
}


I have a file that I created with:
heroku config > .env2

After some editing it worked fine. 

Thanks!!!

ved

unread,
Apr 13, 2018, 5:14:34 PM4/13/18
to Fat-Free Framework
Cool. Note that you can use mset.

$f3->mset([
   
'db_dns' => getenv('db_dns'),
   
'db_user' => getenv('db_user'),
   
'db_pass' => getenv('db_pass'),
   
'db_name' => getenv('db_name')
]);

Same thing, a bit prettier to look at :)
Reply all
Reply to author
Forward
0 new messages