Welcome Guest, Not a member yet? Register   Sign In
Multiple App with .env
#1
Question 

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
Reply
#2

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'); 
Reply
#3

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.
Reply
#4

(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/CodeIgni...m/Boot.php
2. edit index.php to run your Boot class. https://github.com/codeigniter4/CodeIgni...hp#L53-L56
Reply
#5

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
Reply
#6

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));
Reply
#7

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
Reply
#8

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

You need to load the class before using it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB