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 is my implementation for using local and production env:
use Dotenv\Dotenv; $env = $app->detectEnvironment(function () { $whitelist = [ "127.0.0.1", "::1" ]; $envPath = realpath(dirname(__DIR__)); // Production ENV $envFile = ".env"; // If is localhost change to Local ENV if (in_array($_SERVER["REMOTE_ADDR"], $whitelist)) $envFile = ".local.env"; $env = Dotenv::create($envPath, $envFile); $env->overload(); });
As previous you can copy this to bootstrap/evironment.php. As well you can move
$whitelist
toconfig/app.php
and call it from that file:$whitelist = config('app.local_ip'); // or whitelist
Tested on Laravel 6.x ONLY
working on laravel 6
Argument 1 passed to Dotenv\Dotenv::create() must implement interface Dotenv\Repository\RepositoryInterface, string given.
In Laravel 7 Dotenv create method modified. Please help
For the next person wondering that, the documentation exists and you can read this: https://github.com/vlucas/phpdotenv#immutability-and-repository-customization
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
@r4rishabhgupta
detectEnvironment() must be called on $app. Your error suggests that $app is null, which means your $app has not been created yet. If you are including environment.php in bootstrap/app.php, as the original post suggests, then that include must come after $app is created. $app is created by these lines of code:
So, your include of environment.php must come after those lines.