How to load different .env file in laravel 8 - laravel-8

I need to load a different .env file, named .env.staging under certain conditions.
In .env file
APP_NAME="Laravel"
APP_ENV=staging
APP_KEY=
APP_DEBUG=true
APP_URL=
But my .env.staging file is not loaded. How to load different .env

Test my way: Create env folder in root folder with 2 environement files.
Content of env\.env.staging:
APP_NAME="STAGING MODE"
APP_KEY=base64:AjYSIS9myYGnVkdfaXy2Oz6lY/ofyNhuqN9ZtkKaNm0=
APP_DEBUG=true
APP_URL=http://localhost
Content of env\.env.test
APP_NAME="TEST MODE"
APP_KEY=base64:AjYSIS9myYGnVkdfaXy2Oz6lY/ofyNhuqN9ZtkKaNm0=
APP_DEBUG=true
APP_URL=http://localhost
Then set this code to boot file bootstrap\app.php:
<?php
use Dotenv\Dotenv;
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// Load temporary environement variables
$dotenv = Dotenv::createImmutable(__DIR__, '../.env');
$dotenv->load();
// Get dynamic APP_ENV wrote in .env file
$env = $_ENV['APP_ENV'];
// You can debug by command var_dump to check
// var_dump($env);
switch ($env) {
case 'staging':
// Overwrite existing environement variables
$dotenv = Dotenv::createMutable(__DIR__ .'/../env', '.env.staging');
$dotenv->load();
break;
case 'test':
// Overwrite existing environement variables
$dotenv = Dotenv::createMutable(__DIR__ .'/../env', '.env.test');
$dotenv->load();
break;
default:
break;
}
Create a route with a view has this title:
<title>{{ env('APP_NAME') }}</title>
When you change variable APP_ENV in .env file:
APP_ENV=test
You will get the title “TEST MODE”
If you change to:
APP_ENV=staging
You will get the title “STAGING MODE”
The title changed, mean the environement variables are loaded!
Tested with "laravel/framework": "^8.75" version has built-in package "vlucas/phpdotenv": "^5.4.1".

To use the .env.test file try to restructure your initial bootstrap() method inside of the App/Http/Kernel.php file to look like this:
public function bootstrap()
{
app()->loadEnvironmentFrom('.env.staging');
parent::bootstrap();
}

Related

process.env.variable is working in local development but after deploying to heroku its not working in nuxt.js

I am using nuxt.js and trying to show images dynamically. That's why I use BASE_URL variable inside .env file and access image files according to the BASE_URL in my local environment.
.env file
BASE_URL = https://pctool.herokuapp.com
DB_HOST = dummy_host_name
DB_USER = dummy_user_name
DB_NAME = dummy_database_name
PASSWORD = dummy_password
in image.vue file
template
<img class="pic-0" :src="makeImagePath(product.image[0])"/>
script
methods: {
makeImagePath (img_name) {
return process.env.BASE_URL + "/product/" + img_name;
}
}
Local output is
But after deploy the code to Heroku is not working with env variable.
heroku env
heroku env veariables
Deployed output is
But if I make the URL hardcoded like below
script
methods: {
makeImagePath (img_name) {
return "https://pctool.herokuapp.com/product/" + img_name;
}
}
Then it's working both places. Now in that position URL is not dynamic, I have to change the URL from local to production wherever I use that concept in my project. That's why I want to make the URL dynamic so that there was no conflict during deployment.
Generally you should not commit your .env file. In your Heroku app under settings you can set the environment variables.
There you can define these again:
BASE_URL = https://pctool.herokuapp.com
DB_HOST = dummy_host_name
DB_USER = dummy_user_name
DB_NAME = dummy_database_name
PASSWORD = dummy_password
Finally figured it out. If you want to access .env variables client side, you need to define them in the Heroku Config Vars settings, but then you also need to add this inside your nuxt.config.js file, for example:
publicRuntimeConfig: {
baseUrl: process.env.BASE_URL,
originHeader: process.env.ORIGIN_HEADER
}
Then you'll be able to access them like this: this.$config.baseUrl
Of course, be sure you do not expose any private variables like API keys or whatever in this way, as these are client-side and therefore public. Cheers!

CucumberJs cannot find steps using webdriverio5

I have defined wdio.conf.js file (main file) and environment specific dev-chrome.conf.js file.
I can't get get cucumber to recognize my step definitions folder.
This is my structure:
And this is what I have in dev-chrome.config.js file:
const wdioConfig = require('../../../../../wdio.conf.js');
const commands = require('../../../../../src/commands/commands');
wdioConfig.config.cucumberOpts = [{
// other stuff here
require:
[
'./src/step_definitions/**/*.js',
// Or search a (sub)folder for JS files with a wildcard
// works since version 1.1 of the wdio-cucumber-framework
//'./src/**/*.js',
],
// other stuff here
}];
exports.config = wdioConfig.config;
I am getting an error:
"Step "When I add the product to a cart" is not defined. You can ignore this error by setting cucumberOpts.ignoreUndefinedDefinitions as true."
When I have same path for step definitions defined on main wdio.conf.js file then it works.
My main wdio.conf.js file is located in the root folder of the project.
Do you know how could I make it work in the environment specific conf.js file?
I am using #wdio/cucumber-framework": "^5.13.2"
As per the below example config, the cucumberopts should be an object and I think you are trying to set it as an array.
https://github.com/amiya-pattnaik/webdriverIO-with-cucumberBDD/blob/master/test/config/suite.cucumber.conf.js#L156
Maybe you should follow this example which will help to understand config setup.
Cheers!

Data driven test in Nightwatch.js

How to create data driven test using Nightwatch.js?
I am using data.js file as global. The content is:
module.exports={
username:'xyz#gmail.com',
password:'12345',
urls: {
login: 'http://www.asdf.com'
}
};
By giving the your file path value in key "globals_path" of your nightwatch.json file like below, you don't need to rename your js file , you can use data.js file as global file:
"globals_path" : "path/to/data.js",
and in your test file you can call variable like this :
browser.globals.username etc

sails.js less livereload with grunt watch not working

I got my less files compiled in css perfectly by grunt and I see result in .tmp/public/styles
So now livereload with grunt-contrib-watch should be made naturally in sails generated project ?
Or do I have to make a special configuration ?
I found that in tasks/pipeline.js file but not sure of what to do.
// CSS files to inject in order
//
// (if you're using LESS with the built-in default config, you'll want
// to change `assets/styles/importer.less` instead.)
var cssFilesToInject = [
'styles/**/*.css'
];
I saw in the file tasks/README.md :
###### `sails lift`
Runs the `default` task (`tasks/register/default.js`).
And in the file default.js we got :
module.exports = function (grunt) {
grunt.registerTask('default', ['compileAssets', 'linkAssets', 'watch']);
};
But watch.js file is missing in the folder...
What should it be ?
Watch does only looking for files that have changed and execute less, sass, injection and so on - but it doesn't make a reload.
You can add this in task/config/watch.js

Using gon with jasmine-rails

Our backbone app uses gon. When we try to run our tests, we are getting a gon is undefined error in the console of the browser. Our layout file includes a call to include_gon, but that file is not being loaded by jasmine, so jasmine is failing in our first javascript file that contains gon. We tried creating a helper to assign the gon variable to an empty hash (like a fixture), but the helper was called after the first call to gon and therefore didn't fix our issue.
The secret was to use the asset pipeline to define the load order of my files. I commented out these lines from jasmine.yml
# path to parent directory of src_files
# relative path from Rails.root
# defaults to app/assets/javascripts
#src_dir: "app/assets/javascripts"
# list of file expressions to include as source files
# relative path from src_dir
#src_files:
# - "application.{js.coffee,js,coffee}"
And created spec.js.coffee with these lines:
#= require application
#= require jasmine-jquery
Now my js files get loaded in order and I am good to go.
What worked for me was to define window.gon = {} inside the test like so:
describe("A suite is just a function", function() {
beforeEach(function() {
window.gon = {}
gon.test = "this"
})
it ("should find gon", function() {
expect(gon.test).toBe("this")
})
})