To add phpdotenv, I am supposed to run the following terminal command:
composer require vlucas/phpdotenv
But I'm not exactly sure which folder I should be running the command.
My folder structure looks like this:
SITE
--> www
--> css
--> include
- database.php
- index.php
docker-compose.yml
dockerfile
When I run the composer command from the main SITE folder, it creates a folder called Vendor, as well as a composer.json file looks like this:
{
"require": {
"vlucas/phpdotenv": "^5.6"
}
}
So now the structure looks like this:
SITE
--> www
--> css
--> include
- database.php
- index.php
--> vendor
- composer.json
- composer.lock
- docker-compose.yml
- dockerfile
So now I can create a .env file within my www folder with the other webfiles, which looks like this:
DATABASE_HOST=db
DATABASE_NAME=myDb
DATABASE_USER=user
DATABASE_PASS=test
So in my database.php file, I try to pass the info from my .env file:
<?php
require __DIR__ . "/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dbhost = $_ENV["DATABASE_HOST"];
$dbname = $_ENV["DATABASE_NAME"];
$dbuser = $_ENV["DATABASE_USER"];
$dbpass = $_ENV["DATABASE_PASS"];
try{
$dbc = new PDO("mysql:dbname=$dbname;host=$host;port=3306;user=$dbuser;pass=$dbpass");
$dbc->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Conneciton failed: " . $e->getMessage() . "<br/>";
}
?>
After doing all of the above, I am getting the below error:
Warning: require(/var/www/html/include/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/include/database.php on line 2
Fatal error: require(): Failed opening required '/var/www/html/include/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html/include/database.php on line 2
I tried adjusting the vendor path in the database.php file, but I still get the same error.
I tried running the composer command inside the www folder, but I still get the same error.
What did I do wrong and how do I fix it?
I no longer got the error when I moved the Vendor folder, the composer.json and composer.lock into the WWW folder.