CodeIgniter Forums
Multiple App with .env - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Multiple App with .env (/showthread.php?tid=90759)



Multiple App with .env - piieerre - 04-29-2024

Hello everyone
I am looking to use .env environment files with an instance composed of several applications.
For example, I want to have several applications on the same CodeIgniter instance but with different configurations (Example: baseUrl, database, etc.)
How would you proceed?

My structure on CI v4.5.0:

app1
     Config
     Controllers
     Database
     ..
app2
     Config
     Controllers
     Database
     ..
app3
     Config
     Controllers
     Database
     ..

Thanking you in advance.
Pierre


RE: Multiple App with .env - kenjis - 04-29-2024

Define your own Environment Variables, and set the value to corresponding app's config.

E.g.,
In .env file:
Code:
BASE_URL1=https://app1.example.com/


In Config\App's constructor:
PHP Code:
$this->baseURL env('BASE_URL1'); 



RE: Multiple App with .env - piieerre - 04-29-2024

Hello and thank you for your answer
In fact, the idea was not to touch the files contained in the Config folder, because they are often updated.
In previous versions, it was possible to choose a .env file in the public/index.php file.
Now, apparently, this is no longer possible.


RE: Multiple App with .env - kenjis - 04-29-2024

(04-29-2024, 10:47 PM)piieerre Wrote: In previous versions, it was possible to choose a .env file in the public/index.php file.

If you want to do like that,
1. extend the Boot class https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Boot.php
2. edit index.php to run your Boot class. https://github.com/codeigniter4/CodeIgniter4/blob/8b0c99bc8efbd7d50c545cb529e84602ba1ea6b7/public/index.php#L53-L56


RE: Multiple App with .env - piieerre - 04-30-2024

Thank you for your reply

Can you tell me how to proceed? How to extend the Boot class and where to place this file?

Thanks in advance


RE: Multiple App with .env - kenjis - 04-30-2024

app/Boot.php:
PHP Code:
<?php

declare(strict_types=1);

namespace 
App;

use 
CodeIgniter\Boot as FrameworkBoot;
use 
CodeIgniter\Config\DotEnv;
use 
Config\Paths;

/**
* Bootstrap for the application
*
* @codeCoverageIgnore
*/
class Boot extends FrameworkBoot
{
    /**
    * Load environment settings from .env files into $_SERVER and $_ENV
    */
    protected static function loadDotEnv(Paths $paths): void
    
{
        require_once $paths->systemDirectory '/Config/DotEnv.php';
        (new DotEnv($paths->appDirectory '/../'))->load();
    }


Code:
--- a/public/index.php
+++ b/public/index.php
@@ -52,5 +52,6 @@ $paths = new Config\Paths();

// LOAD THE FRAMEWORK BOOTSTRAP FILE
require $paths->systemDirectory . '/Boot.php';
+require $paths->appDirectory . '/Boot.php';

-exit(CodeIgniter\Boot::bootWeb($paths));
+exit(App\Boot::bootWeb($paths));



RE: Multiple App with .env - piieerre - 04-30-2024

Thanks for your response

But I have an error :
PHP Fatal error: Uncaught Error: Class "CodeIgniter\\Boot" not found in /var/www/vhosts/domain.com/app.domain.com/app/Boot.php:16


RE: Multiple App with .env - kenjis - 04-30-2024

I recommend you learn how PHP works if you use CodeIgniter.

You need to load the class before using it.