How to Get Environment Value in Config File - laravel

Currently, I have an environment variable in my .env file.
MY_NAME_TEST=Testing
How do I pass that variable as part of a path in a config file? I'm trying to get a path similar to below:
base_path('Plugins/Testing/Database/Migrations/')
Notice the "Testing" part of the path in the sample above; I need that name to be different each time.

You're able to retreive the values anywhere in your laravel project by making use of the env function.
env('MY_NAME_TEST'); // returns "Testing"
In your case it would be used like so:
base_path('Plugins/' . env('MY_NAME_TEST') . '/Database/Migrations/'
The second parameter the env function takes is a default value if a value is not already set.

Related

Override Cypress.config().baseUrl with environment variable (process.env)

I was to set the baseURL of my cypress tests to be read from .env file.
I can't set it directly in the cypress.json file.
And when I try to use cy.visit(process.env.MYAPPURL), I get an this error
cy.visit() must be called with a url or an options object containing a
url as its 1st argument
you can leave cy.visit() empty but you just need to set this env CYPRESS_BASE_URL with the base url instead like this for example:
CYPRESS_BASE_URL=$VUE_APP_BASE_URL
check this for explanation https://docs.cypress.io/guides/guides/environment-variables

How to pass a parameter as an environment variable

How do you pass a parameter as an environment variable?
Step 1: Open up user bash file 'vim ~/.bash_profile', write the environment variable and save the file
export TWLIO_NUMBER=+303....
Step 2: In the application porperties file, store the variable
twlio_number=${TWLIO_NUMBER}
Step 3: Import Value in order to use it
import org.springframework.beans.factory.annotation.Value;
#Value("${twlio_number}")
private String TWILIO_NUMBER;
Also, if I hardcode the value in the application properties file, the code works.
Pass those as Java-Opts. It will work.
How to pass JVM arguments in SpringBOOT
https://www.baeldung.com/spring-boot-command-line-arguments
So I have a system variable in my spring project.
String dataSourceUrl = System.getenv(DEFAULT_CONNECTION);
I am looking for something quick to set the variable in command line.
I found this command work in mac
export DEFAULT_CONNECTION=value (value wrap in single quote to prevent the values being replaced)
You can check by env | grep DEFAULT_CONNECTION
run as usual mvn spring-boot:run

env helper returns null even if providing default value

Inside my .env I got these two
ATLAS_RELEASES=null
ATLAS_DOWNLOAD=null
Inside my own config file I have this
'releases_url' => env('ATLAS_RELEASES', $baseUrl . 'atlas/raw/master/releases.json'),
'download_url' => env('ATLAS_DOWNLOAD', $baseUrl . 'atlas/releases/download'),
Once some code runs, that uses
$relasesUrl = config('releases_url');
$downloadUrl = config('download_url');
null is returned instead of the second option, which I've specified in the config file. Since I'm not using .env outside of config files, I wonder why I get this behavior?
The above only works if I remove these two completely from the .env file
ATLAS_RELEASES
ATLAS_DOWNLOAD
Any ideas why this strange behavior happens?
As long as the key exists in the .env file no matter if if is null or no value at all, that one will be used.
https://laravel.com/docs/6.x/configuration#retrieving-environment-configuration
The second value passed to the env function is the "default value". This value will be used if no environment variable exists for the given key.

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?

Using user defined variables in the path of a results file in JMeter

I want to write a results CSV file in JMeter which contains a variable in path of the file I write.
E.g.
C:\\Users\\User1\\test-results\\${output}.csv
But I only seem to be able to use predefined variables like ${__time(ddMMyyHHmmss)}
Is there a way to use user defined variables in the path? I have successfully done this to find input files by defining the variable in the test plan node as a User Defined Variable.
I managed to use user defined variable in the path of result file using JMeter 2.9. REPORT is a user defined variable with value REPORT. It gives me file named REPORT.csv
In JMeter 3.1(?) (or Windows?) a double slash is required in the path.
I have used the following successfully:
c:\\jmeter\\results\\${testId}\MyReport.csv
c:\\jmeter\\results\\${testId}\MyReport.csv
c:\\jmeter\\results\\${__time(yyyyMMddHHmm)}\MyReport.csv
c:\\jmeter\\results\\${__time(yyyyMMddHHmm)}.csv
${testId} is a User Defined Variable configured in the Test Plan and set to ${__time(yyyyMMddHHmm)}

Resources