5

I'm using Laravel 5.2 and I do php artisan config:cache as recommended in the official documentation for speed improvement.

As you may know, this command makes the .env file variables directly inaccessibles (you can test it with php artisan tinker), and for that reason all the calls to env() and getenv() function must be replaced by config() in the code, except for the files in the config folder. After executing this command, calls like env('APP_ENV') return NULL.

In my project, I'm connecting to Google Cloud using the google-auth-library-php. Unfortunately, in the CredentialsLoader.php file there is a call to the function getenv(self::ENV_VAR) that attempts to get the path of the Google credentials file. As I run the command php artisan config:cache, the path can't be read from the .env file and the connection cannot be completed.

I can see 3 ways to continue:

  1. Forget about running php artisan config:cache.
  2. Ask here if somebody knows how to specify the path of the Google credentials file as a parameter of any function of the package.
  3. (Forgive me, God) Correct the CredentialsLoader.php file (getenv() to config()), run the command and track this file in the repository, then this change will propagate to every team member when they pull.

Thank you in advance!

rsebjara
  • 51
  • 2
  • 6
  • It's `CredentialsLoader::fromEnv` that loads from env. It makes sense to use another method on CredentialsLoader, or create the credentials thingie manually using settings in your config. – sisve Sep 25 '17 at 04:50
  • Hi! has been 3 years now... :) What was your solution? – Lorenzo S Oct 20 '20 at 13:43
  • Well actually I did it with a particular configuration... Nignx + php with fastcgi. I will answer because maybe can be of any help if in the future someone will visit this question again – Lorenzo S Oct 20 '20 at 14:21

2 Answers2

1

For those who still encounter this error, you can specify the file using keyFile index in the first parameter:

$storage = new StorageClient([
   'keyFile' => json_decode(file_get_contents($path), true),
]);

Or you can specify the .json file directly to config using keyFilePath index

$storage = new StorageClient([
   'keyFilePath' => '/path/to/json/CompanyDataStorage-foobarbaz.json',
]);
Player1
  • 2,878
  • 2
  • 26
  • 38
0

That's a very old question but maybe it can be useful for someone coming for the same problem even in the newer versions of Laravel. Basically I don't have a solution that works for all the possible environments, but it gives the idea. My environment runs php7.4-fpm with NGINX. If you are using the same environment, you will have a location directives for your php files. Nginx can set FastCGI parameters with fastcgi_param and you will have probably used it with SCRIPT_FILENAME etc.. You can can then add

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    [...]
    fastcgi_param GOOGLE_APPLICATION_CREDENTIALS "/absolute/path/to/keyfile.json";
}

Then just reload Nginx (ex. for Ubuntu 20.04, sudo systemctl reload nginx).

For Apache the same idea would probably work with SetEnv https://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv

Lorenzo S
  • 1,397
  • 13
  • 25