Laravel - How to declare a file path constent in .env file? - laravel

In one of my Laravel based application, I want to include a JSON key file that is currently located in public/key/store.json
Now I want to write a constant in Laravel .env file so that I can access that file anytime I want. So, I write the following line of code:
KEY_FILE='/public/key/store.json'
But it show file path does not exist.
Can anyone tell me what's wrong in my declaration?

you should push your path in your app/config.php not in your .env

Related

How to use environment variable(string) in a .conf file

I have a .conf file that has a label and variable that I'm trying to move to a .env file. How can I accomplish this?
This is what I have in my .conf file
[[inputs.snmp]] #Label
agents = ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"] #Variable
version = 2 #Variable
I'm trying to have something like this in the .env file
VAR_FOR_CONF_FILE='[[inputs.snmp]]\n agents= ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"]\n version=2'
I was hoping I could use $VAR_FOR_CONF_FILE in my .conf file instead of [[inputs.snmp]]agents = ["1.1.1.1:111","2.2.2.2:111","2.3.3.3:111"]
but I keep getting this error Error parsing data: line 7: invalid TOML syntax for $VAR_FOR_CONF_FILE (Im not sure if I'm getting this error because I have the syntax wrong in my .env file or I'm declaring $VAR_FOR_CONF_FILE incorrectly in my .conf file)
Am I doing it correctly (or is it even possible to do what I'm trying to accomplish)?
(Note: I'm trying to accomplish this so I can simply use $VAR_FOR_CONF_FILE instead of hard coding things in the .conf file)
I'm trying to use VAR_FOR_CONF_FILE in the .conf file
If the .conf file parsing program doesn't support variable substitution, there's no way around modifying the .conf file, but this can be automated:
sed -i "s/\<VAR_FOR_CONF_FILE\>/$VAR_FOR_CONF_FILE/" my.conf

How do I connect .env file to config.yml?

I'm currently building a Shopify store and would like to use env variables in Themekit's config.yml file. What I'm confused about is how to connect the .env file to the yml file, since I don't think you can just require dotenv. I have my .env file, and the code below in the config.yml. Thanks!
password: ${DEV_PASSWD}
theme_id: ${DEV_THEMEID}
store: ${DEV_SHOP}
You can't include .env file inside a YAML one. However, you can interpolate variables into your config.yml file using the ${} notation.
To help you interpolate variables, there are special files that can be used to automatically to load environment variables for Theme Kit. The following table lists the file paths for each operating system:
macOs: ${HOME}/Library/Application Support/Shopify/Themekit/variables
Linux/BSD: ${XDG_CONFIG_HOME}/Shopify/Themekit/variables
Windows: %APPDATA%\Shopify\Themekit\variables
Even more, you can use the --vars flag in any command to provide a path to a file for loading variables. The variables file has the same format as most .env type files. But note, the .env file is not interpolated by YAML itself and it cannot be connected using standard YAML include directives. All magic is provided exclusively by shopify and its --vars flag.

Use multiple env files

I'm wondering if there's a way in Laravel to specify a set of env files to load. My exact problem is I want to add something like a suffix to all my .js and .css resources. Ideally I'd have a suffix like the release date because it would be ok for these files to be cached within the same release but I would like the caches to be invalidated on the next release. However I want to avoid reading, modifying and saving the .env file if possible and would instead prefer to create a new file e.g. .env.rdate which would be generated via a script, e.g.
echo APP_RELEASE_DATE=`date +%s` > env.rdate
or something like this. Is this at all possible or do I have to read/update/write the .env file instead?
Create your .env.rdate file next to .env file.
Put this to your AppServiceProvider boot method:
$dotenv = new \Dotenv\Dotenv(base_path(),'.env.rdate');
$dotenv->overload();
After you can use in your project:
ENV('APP_RELEASE_DATE')

How to set path to file in Laravel?

I have located file in root directory of Laravel.
From controller I try to get this file like as:
$shipments = json_decode(file_get_contents("Statistics.json"), true);
But I get error.
What correct path to specify for getting file?
https://laravel.com/docs/5.0/helpers#paths
I guess you need base_path
file_get_contents(base_path().'/Statistics.json');

Laravel 5 doesn't read values from dot ENV files

I don't know if this question is relevant or not. LARAVEL 5 is still in developmental phase. I have pulled LARAVEL 5 after watching one of the Laracast video about new features in LARAVEL 5. I couldn't resist to wait for its formal release.
I named the local environment dot file as .env.local.php. But for some reason I am unable to get the the values from this dot file when using $_ENV['KEY'].
I am quite sure that I have configured the environment correctly. When doing $app->environment() shows the correct environment. Has it been changed in LARAVEL 5 the way we get the values from dot files or am I missing something ?
By default in environment.php file you have something like that:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
}
so only .env file is being read (notice .env not .env.php - so you should rename your file - or you can add as 2nd parameter file name .env.php if you want). Any other environment files (.local.env) are not being read by default - you will need to load them manually.
If you don't have such code by default, you should probably update/install Laravel 5 again (changes appear very often)
Now, I don't know what method you use, but you can put in your .env file also your environment name in for example APP_ENV variable, create .local.env file with content you want and then you could use in environment.php file:
if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
echo "loading";
Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
}
}
If you don't want to do it this way, you can probably change the other and load env file you want based on $env assuming you use PC based environment detection.
If it's unclear you can also look at What's the correct way to set ENV variables in Laravel 5?

Resources