Ok, first, I do not know what library you are using to read your file .env
, but assuming it is PHPDotenv or Symfony / Dotenv, what you do in your local is to export the contents of that file to environment variables, and then access them in the life cycle of the application with getenv
or $_ENV
.
In Heroku the environment variables are handled in the application panel. The best practices indicate that your file .env
should not be subject to version control but explicitly ignored, to avoid that the keys remain in github or wherever. Probably, as Kleith said, Heroku is simply ignoring that file.
For the project to read the environment variables, go to your Heroku project, to the configuration section of your app (it can be in link ) and click REVEAL CONFIG VARS
.
Enter the content of your .env
here (here I put two variables as an example, you must put them all. Without quotes .In a file .env
everything that comes after =
it is assumed to be text so you do not need them.
With that your app should be able to connect to the database. But there is another point. In your local the swagger definition of the models is in the root of the project and the routes in /api/v1/<endpoint>
, while in Heroku they are under public
. (For example, /public/api/v1/<endpoint>
).
To modify this behavior, you have to add (or modify if you already have it) a Procfile
file to the root of the project . Note that it is Procfile with a capital letter.
The content of your Procfile
, considering that you are using Apache, should be:
web: vendor/bin/heroku-php-apache2 public/
With those two steps:
Enter the environment variables
Create or modify the Procfile using public
as document root
You should be able to use the app as you use it in localhost.
Optional step
I would recommend removing the .env
file from version control using git rm --cached .env
and then adding .env
to your .gitignore
file.
However, it is possible that in your application you are hard-calling the file .env
without verifying that it exists, something like:
$dotenv->load(__DIR__ './../.env');
If you remove it from version control that's going to throw you an error. You should wrap that call in a check that the file exists:
if(is_readable(__DIR__ './../.env')) {
$dotenv->load(__DIR__ './../.env');
}