I need to have more than one .env files like .env, .env.local, ....
and need to be able set up proper env file before application is created.
I tried to do it via --env param but it seems it does not work with
php artisan serve --env local because any request from client to localhost:8000 have original .env file.
For other artisan commands this workd but not for artisan serve.
There is a Application::environmentFile() method but I can call it at the very end of Application set up process.
Can somebody tell me please how to do it properly?
You only need one .env per environment. You don't need to have multiple.
Your .env is created on the server you are deploying to so on a staging server, the .env will be different to the one on the production server.
The .env file should never be committed to your repository, but you can create an env.example which has placeholders to prompt what needs filling in on other servers or for other developers.
@Čamo That completely defeats the point of .env files.
Their entire purpose is to set environment variables for an environment, where setting environment variables proper is difficult. So, locally, you would have an .env with values specific to your local environment. Other environments would then have their own .env files with values appropriate to that environment only.
I understand your point to have only one ev file and do not commit this file to git. BUT I really need it in Git so I would like to have more than one env file. For now I have this solution in bootstrap/app.php but I am not sure if the call of ->create()->loadEnvironmentFrom(getEnvFile()) is in the right order.
<?php
use App\Services\ServerErrorsLogService;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
function getEnvFile()
{
return file_exists(__DIR__.'/../.env.local') ? '.env.local' : '.env';
}
$app = Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
// This code is used to register middleware for the application
})
->withExceptions(function (Exceptions $exceptions) {
// This code catch all exceptions and try to save them to the local_server_errors SQLite database
$exceptions->report(function(Throwable $e) {
/** @var ServerErrorsLogService $localServerErrorsLogService */
$localServerErrorsLogService = app(ServerErrorsLogService::class);
$localServerErrorsLogService->saveServerErrorToLocalDisc([
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
]);
});
})
->withSchedule(function ($schedule) {
// Use console.php for scheduling jobs
})
->create()
->loadEnvironmentFrom(getEnvFile());
return $app;
I understand you very well. BUT we are not talking here about common web application or API but about devices like Raspberry PI, Firefly, .... and every device could have its own specific environment which are related to specific version of the system. So it is not only about to copy .env from current repository but about specific env file for specific version......
I understand you very well. BUT we are not talking here about common web application or API but about devices like Raspberry PI, Firefly, .... and every device could have its own specific environment which are related to specific version of the system. So it is not only about to copy .env from current repository but about specific env file for specific version......
@Čamo This is literally what environment variables are for.
Set the variables in those environments to the values you need, and it makes .env files completely redundant.
.env files only exist for scenarios where it’s difficult or impractical to set environment variables, such as your local machine where you may be running multiple projects.
@martinbean Ok so tell me please how to do it for all variables in env file. Where should I put the code cause nor the ChatGPT does not know. And how to ensure that Laravel do not overide my variables.
@Snapey i am just currious how to do it. Composer is one of the way I did not think about.
Now I have something like this in app.php and it seems to work.
$basePath = dirname(__DIR__);
$envFile = file_exists($basePath.'/.env.local') ? '.env.local' : '.env';
// 🚀 THIS HAS TO BE HERE BEFORE APPLICATION IS CREATED
Dotenv::createImmutable($basePath, $envFile)->safeLoad();
@martinbean I tried to use something like in app.php
$basePath = dirname(__DIR__);
$envFile = file_exists($basePath.'/.env.local') ? '.env.local' : '.env';
// 🚀 THIS HAS TO BE HERE BEFORE APPLICATION IS CREATED
Dotenv::createImmutable($basePath, $envFile)->safeLoad();
This works fine but the device has to be selected with better precission. It will check what kind of device is running and then choose right .env file.
@Čamo How can it? How does the application know that its running on a raspberry Pi and therefore it needs .env.raspberry (for example)
If you HAVE to have different files in one distribution, have a step in your composer.json that copies the relevant env.various into .env as a one-off deployment step for that individual machine.
Go on, tell me... you don't use a deployment script, you FTP the files?
@Snapey Imagine hundreds of devices which have different motherboard different system versions .... I need to know what the application is running dynamically.