Last active
February 29, 2024 04:53
-
-
Save msankhala/3c282abf546318a2662e to your computer and use it in GitHub Desktop.
Setup Multiple Environment for Laravel 5 Developers Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Follow this instructions: | |
|-------------------------------------------------------------------------- | |
| | |
| Laravel takes a dead simple approach to your application environments | |
| so you can just specify a machine name for the host that matches a | |
| given environment, then we will automatically detect it for you. | |
| | |
| 1. Check if you have .env file (it should be automatically created by laravel) | |
| | |
| REMOVE all the contents from here, and just put: local or production | |
| (whatever environment you want). This will be the one that will be | |
| changed when you want to switch to another environment. :D | |
| | |
| 2. Create new Environment file let say you have your local and production enviroment. | |
| | |
| Create a file with the name of: .local.env | |
| Create a file with the name of: .production.env | |
| | |
| 3. Add default environment value. | |
| | |
| For Local Environment (.local.env file) | |
| APP_ENV=local | |
| | |
| For Production Environment (.production.env file) | |
| APP_ENV=production | |
| | |
| 4. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php | |
| – Inside of this file we will do the magic. Insert this snippet code. | |
*/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Detect The Application Environment | |
|-------------------------------------------------------------------------- | |
| | |
| Laravel takes a dead simple approach to your application environments | |
| so you can just specify a machine name for the host that matches a | |
| given environment, then we will automatically detect it for you. | |
| | |
*/ | |
$env = $app->detectEnvironment(function(){ | |
$environmentPath = __DIR__.'/../.env'; | |
$setEnv = trim(file_get_contents($environmentPath)); | |
if (file_exists($environmentPath)) | |
{ | |
putenv("APP_ENV=$setEnv"); | |
if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) { | |
Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env'); | |
} | |
} | |
}); | |
/* | |
| | |
| 5. This snippet code, use, putenv php function to set APP_ENV and then using | |
| the Dotenv package by vlucas. I’m loading the file that we created and | |
| base on the .env file we can determine which environment should we use. | |
| | |
| 6. Include your environment.php file in bootstrap file. Paste it inside your | |
| bootstrap/app.php file. | |
| | |
*/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Load Environment File on Startup | |
|-------------------------------------------------------------------------- | |
| | |
| This will determine, which environment will be loaded for our application. | |
| | |
*/ | |
require __DIR__.'/environment.php'; | |
// 6. After that you can check which environment is being loaded using: | |
App::environment() | |
// 7. I output the current environment in my Laravel 5 Welcome Page, You can see my screenshots below: |
This works for me on Laravel V8
<?php
use Dotenv\Dotenv;
$env = $app->detectEnvironment(function(){
$environmentPath = __DIR__.'/../.env';
if (file_exists($environmentPath))
{
$setEnv = trim(file_get_contents($environmentPath));
putenv('APP_ENV='.$setEnv);
$dotenv = Dotenv::createImmutable(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
$dotenv->load(); //this is important
}
});
In Laravel 10, at the end of bootstrap/app.php
I did this to include an ADDITIONAL .env
file that I wanted to use
$app->afterLoadingEnvironment(function (Illuminate\Foundation\Application $app) {
Dotenv::create(Env::getRepository(), $app->environmentPath(), '.env-overrides')
->safeLoad();
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the next person wondering that, the documentation exists and you can read this: https://github.com/vlucas/phpdotenv#immutability-and-repository-customization