Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel How to define a file location on .env?

Tags:

php

laravel

I have a .json file that I want to define on .env. How do I do that ? my json file is on (laravel root folder)/json_google/api_key.json . I tried GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=/json_google/api_key.json but doesn't work ..

like image 886
Billy Adelphia Avatar asked Dec 08 '22 16:12

Billy Adelphia


2 Answers

Laravel has many helper functions to resolve the path. Such as base_path() in your case. You'll find all helpers here: vendor/laravel/framework/src/Illuminate/Foundation/helpers.php

base_path(env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION');

However, I'd set the full path instead of a relative path. Otherwise your application has to know what it should be relative to (laravel root, or app path, ...) and take this into account, which kind of defeats the purpose of putting the file location outside your code base, as it makes it harder to move it to some place else.

As the .env file is per deployment, there is no problem with setting the full path.

like image 161
Stefan Avatar answered Dec 10 '22 21:12

Stefan


You should use .env variable using constants file.

.env file

GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=/json_google/api_key.json

Now in config/constatns.php set

return [
       'STORAGE_GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION' => env("GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION", ""),
];

Fetch json file path

Config::get('constants.STORAGE_GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION')
like image 37
Sunny Doshi Avatar answered Dec 10 '22 22:12

Sunny Doshi