Maybe I was using it incorrectly, but when I used vlucas/phpdotenv with more than 1 website on the same server, using different credentials or different databases, they seemed to overwrite each other.
So, if a request came in on the first website and then a request on the other website before the first request was completed, the creds from the second request would be used if they were needed in the first request, causing issues.
Because of this issue, I prefer to use a simple php array and I use selective/config to access the items. I use an example settings file and have composer or the user copy it for use.
I hope that answers at least part of your question.
Thanks again to @darkalchemy for the solution.
I tried to implement selective/config, and, for today, it works in my Slim 4 project. Especially, âInjecting the configurationâ is very convenient.
In my project, base url of some image is concatenated with imageâs name. These imagesâ names are written in MySQL DB.
So, to get imagesâ names and base url together in server response.
phpdotenv (itâs not work):
$baseUrlImg = getenv(âBASE_URL_IMAGEâ);
$sql = âSELECT id, CONCAT (â$baseUrlImgâ,âproduct_img_dir/â, img) AS img, name FROM productsâ;
selective/config (it works)
$baseUrlImg = $this->config->getString(âbase_url.base_url_imgâ);
$sql = âSELECT id, CONCAT (â$baseUrlImgâ,âproduct_img_dir/â, img) AS img, name FROM productsâ;
Sorry, but I still to wonder. Is it safe enough to keep sensitive credentials in settings.php of Slim 4 project?
I think as long as you donât commit settings.php to git repo and itâs not in the root folder of your website and your webserver is set up correctly, itâs as safe as it can be.
A typical application begins with three environments: dev (for local development), prod (for production servers) and test (for automated tests).
Each environment differs only somewhat from others. This means that all environments share a large base of common configuration, which can be stored in a file like: config/defaults.php.
While developing, you want to log everything and expose nice debugging tools;
After deploying to production, you want that same application to be optimized for speed and only log errors.
These settings can be stored in environment specific files, like config/development.php and config/production.php. Please note: You must not store sensitive password in this files.
To store the secret credentials you should use a special file like env.php. This file should be excluded from the version control and must never be commited into the git repo.
Then you merge all these 3 files in this order into a single array:
In config/settings.php:
Load config/defaults.php
Load if exists: config/env.php or ../../env.php (on your prod server)