Use multiple env files - laravel-5

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')

Related

Laravel Queue Worker

I have added this woker in etc/superviord.config for one project, I would like to make it work for multiple projects I mean for xyz, project also so should I need to copy and paste below all code or just have to add only one more command line?
Yes, you need to copy-paste your program definition to separate supervisor processes.
And about the folder. You should use the /etc/supervisor/conf.d/.
Just create the config file with SOMETHING-LIKE-A-PROJECT-NAME.conf and put your program definition into it.
And then check that your /etc/supervisord.conf contains the include section:
[include]
files = /etc/supervisor/conf.d/*.conf

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

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

Is there any way to use dotenv with Bitbucket Pipelines?

As the title says, is there any way to use dotenv with Bitbucket Pipelines for CI purposes, while still adding the (perhaps multiple) (.stage).env to .gitignore?
I know Pipeline supports environment variables, and that they can be referenced in bitbucket-pipelines.yml, but I can't figure out how to use dotenv files instead, and vary which file to use based on i.e. branch patterns.
For example, I'd like commits to develop to use .test.env variables, while commits to master instead uses the variables from .prod.env.
Perhaps I'm going down the wrong path? Although other websites use examples of multiple .env files, the library authors discourage that approach. I'm using Zeit Now for hosting, so I can't just SSH a .env file onto the server.
Any advice is very welcome :-)
Create a base64 string out of your .env file. Then copy this string into your environment variables of your pipeline, see here: https://confluence.atlassian.com/bitbucket/environment-variables-794502608.html
For example, your content is now defined in APP_ENV, then you can use this line in your pipeline configuration file:
echo $APP_ENV | base64 --decode --ignore-garbage > ./www/.env
Now it is save because nobody knows your secrets in this file except your pipeline container itself.
This method could be used for all .env-files, also staging files. :)
Rename the files inside your develop pipelines:
mv .test.env .env
or in your master pipelines:
mv .prod.env .env

Ruby hiding API keys and IP address?

I have a ruby script main.rb which takes in two parameters, ipaddress and apitoken.
$token = "VALUE"
$ip_addr = "ADDRESS"
These values are hard coded into the script. When I push the project into Github's repo, I get a warning that my keys are visible.
What is the recommended way to hide these variables? Is it as simple as adding a separate file for these values and adding them to .gitignore?
Personally, I don't like using open and file operations in code. Better way would be to use one of the following approaches,
Put the keys in system environment as follows,
export MY_TOKEN=xyz
export MY_IP_ADDR=a.b.c.d
If you want it to be available after you restart shell, then put it in ~/.bash_profile.
and in your code use as follows,
$token = ENV["MY_TOKEN"]
$ip_addr = ENV["MY_IP_ADDR"]
OR
You can use dotenv gem, if you don't want system wide environment variables and exclude .env from git but putting the file in .gitignore.
Following this guide, a simple way to do this is to create folders .auth_token and .ip_addr.
Add the necessary keys in them and access them by reading the files as follows:
$token = open("lib/assets/.auth_token").read()
$ip_addr = open("lib/assets/.ip_addr").read()
If pushing to a repository, make sure the folders are added to .gitignore

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