You just need to set the local environment variable "APP_ENV" in your .env file to whatever you like. .env.example gives you an idea: https://github.com/laravel/laravel/blob/develop/.env.example
L5 .env how to put test env in the same folder
Earlier we just could make .env.local.php .env.test.php and trsting scripts would pick up testing environment where scripts truncate tables and do havoc while on local I would actually like to preserve data that we pull from backup of production database.
Hi @bart, according with this question I have the following:
If you need to use an .env general file but you also need an .env testing file for Codeception testing environment. What would be the steps needed to create a testing environment? In the last version of Laravel you could create local folders for Local environment and leave the general config/app.php, database.php for production environment.
Thanks in advanced.
I appreciate if someone can shed some light on this. I remember when Jeff was working on behat integration he mentioned load environment from file function.
You can easily achieve that by doing:
$app->loadEnvironmentFrom('.env.test');
// or
App::loadEnvironmentFrom('.env.test');
Hi @kfirba thanks for your reply,
Where did you call this function?
@codeatbusiness Depends what you are trying to achieve. If it's for testing purposes, you can do that before the tests you call that method.
Ok, many thanks @kfirba.
It's still not clear where I should put that code. I tried:
class ExampleTest extends DbTestCase {
public function setUp()
{
parent::setUp();
App::loadEnvironmentFrom('.env.test');
}
}
But then I get:
......PHP Fatal error: Call to undefined method Illuminate\Foundation\Application::loadEnvironmentFrom() in /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 210
I having the same problem, I'd like to create a test .env file and use for testing purposes while I maintain the general .env file. I don't know if is possible that.
Well, that's a question I'm asking myself currently, too. The new DotEnv conecept is simple, but the question is, if it is able to handle same variables for different environments, like it did before. Maybe you have to replace the .env file for testing and re-replace it by the original one afterwards. @JeffreyWay could you bring some light into the darkness, please?
Another question. In L4 I can set different app.php to use different providers/aliases array for develop and production environment. But how to do this in L5 .env
This is exactly what I was trying to find out. For behat package is loading default App::loadEnvironmentFrom('.env.test'); if we do phpspec, unit testing I would like to create few env.beta env.mama env.behat env.jeffrey
You could check the $_SERVER keys and then use $app->loadEnvironmentFrom() accordingly. You'll want to do this logic early, so maybe put it in bootstrap/app.php.
Kind of relevant topic so thought I'd mention that cascaded config files only work now if every single file is present, and they won't fall back to the main configuration in config/. It causes weird errors trying to migrate from a previous laravel dev version, so maybe the best solution here is to eliminate cascaded config folders for distinct environments entirely. I didn't read anything about this before, so it was surprising to see as I liked the old one better.
To expand on @JeffreyWay's comment, right after the app is created in app.php, you want to put something along the lines of this:
// Detect env
$testServers = ['YOUR LOCAL URL'];
if (isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], $testServers)) {
$app->loadEnvironmentFrom('.env.testing');
}
Same if you want to emulate production or something like that.
If I have to use API to check the environment (like in AWS you can check for instance tag) and if I add the code to load specific .env file (for development, test, prod etc) using loadEnvironmentFrom as per @JeffreyWay's comment above, then what caching scheme can I use here in bootstrap/app.php? So that I do not have to run the code for every request? I want to minimize operation of running the api to detect my environment in bootstrap/app.php. And as per my knowledge I will not be able to use the session or other facade in bootstrap/app.php.
Please or to participate in this conversation.