-1

Im new to this, and have been trying for a few hours to get this to work. I have a project with this structure:

/API
    -.env
    /Processor
        -index.php

The contents of my .env file is this:

API_KEY=xxxx

and in my index.php file

require __DIR__ . '/../vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
$dotenv->load();
$client = Client::withApiKey($_ENV['API_KEY']);

It works if I replace $_ENV['API_Key'] with the actual key, so I believe the rest of the setup is correct. I feel like Im missing the correct path to the .env file - how do I check and or fix?

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • 1
    According to [the docs](https://github.com/vlucas/phpdotenv), you shouldn't use the constructor directly. It looks like the usual usage is `Dotenv\Dotenv::createImmutable(__DIR__.'/../');` which returns an object you then call `->load()` on – rickdenhaan Oct 09 '20 at 20:51

1 Answers1

3

You are using Dotenv wrong. It has a static function to create instance.

 $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '\..');
 $dotenv->load();
Jakub Truneček
  • 8,800
  • 3
  • 20
  • 35