Welcome Guest, Not a member yet? Register   Sign In
Multi applications with same app folder but different configurations
#1

(This post was last modified: 11-17-2022, 07:27 AM by serialkiller.)

I need to have a system with a single app folder but multiple applications that use it
I started from the example found in the guide: https://codeigniter4.github.io/CodeIgnit..._apps.html

The structure represented in the example is:

foo/
    app/
    public/
    tests/
    writable/
    env
    phpunit.xml.dist
    spark
bar/
    app/
    public/
    tests/
    writable/
    env
    phpunit.xml.dist
    spark
vendor/
    autoload.php
    codeigniter4/framework/
composer.json
composer.lock

In my case, however, I want to achieve something a little different, not having an app folder for each application, but one for all applications like this:

app/

foo/
    public/
    tests/
    writable/
    env
    phpunit.xml.dist
    preload.php
    spark
    builds
bar/
    public/
    tests/
    writable/
    env
    phpunit.xml.dist
    preload.php
    spark
    builds
vendor/
    autoload.php
    codeigniter4/framework/
composer.json
composer.lock

To get this situation working I did some tweaks to change the pointer path correctly to the new app folder location

In Config/Paths.php
PHP Code:
public $systemDirectory __DIR__ '/../../vendor/codeigniter4/framework/system'

In foo/public/index.php and bar/public/index.php
PHP Code:
require FCPATH '../../app/Config/Paths.php'

In foo/spack and bar/spark
PHP Code:
require FCPATH '../../app/Config/Paths.php'

To get this to work I had to move the writable folder and the .env file to the same level as the app folder

Now I have the two apps working using the same app folder (in the future it will be more than two)

There are a number of things that I wish I could fix but I don't know how

1) Having the writable folder at the same level as the app folder, the various logs will all be inserted here and I will not have the distinction between the various applications, I know that the path of the logs can be changed, but to make them point to the specific application I should insert a variable in the path and I don't know how to do it

2) Same thing for the database, since it has to be different for each application I don't know how to dynamize this value

3) Same thing for $baseURL in the Config/App.php file, each application has its own and it's not the same for everyone

4) I use my own config file with a number of general setting variables, most are the same, but some are supposed to be different

The best thing for me would be to have my config file with some dynamic variables based on the application and use these values to set up the database for each application or to put the path to the writable folder inside its folder and so on.

The .env file could also stay at the same level as the app folder, use it for configurations that don't vary from application to application

In CI3 I have the working structure, but in CI4 many things have changed and I don't know how to reproduce the same situation

Could you help me get this structure working?
Reply
#2

Maybe I found a solution, I just want to understand two things.
I've slightly modified the structure like this, but just for my convenience

app/
website1/
    .env
    builds
    phpunit.xml.dist
    preload.php
    public/
    spark
    tests/
    writable/
website2/
    .env
    builds
    phpunit.xml.dist
    preload.php
    public/
    spark
    tests/
    writable/
vendor/
    autoload.php
    codeigniter4/framework/
composer.json
composer.lock


To solve the $baseURL problem for the different applications I added these 2 lines in Config/Constants.php:


PHP Code:
$base_url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' 'https://' $_SERVER['HTTP_HOST'] : 'http://' $_SERVER['HTTP_HOST'];

defined('BASE_URL_DYNAMIC') || define('BASE_URL_DYNAMIC'$base_url); 


And then to Config/App.php

PHP Code:
public $baseURL BASE_URL_DYNAMIC


Now, for variables that differ between application and application I opted to use the .env file.

In order to use one .env file for each application (see structure), I had to change a line on public/index.php and spark

In index.php I created a constant to get the application folder

PHP Code:
defined('WEBSITE_PATH') || define('WEBSITE_PATH'basename(realpath(__DIR__ DIRECTORY_SEPARATOR '..'))); 


I then use it both in index.php and in spark to load the .env file dedicated to each individual application

I changed this

PHP Code:
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); 


to this

PHP Code:
(new CodeIgniter\Config\DotEnv(ROOTPATH DIRECTORY_SEPARATOR WEBSITE_PATH))->load(); 


At this point having an .env file for each application, the problem of the database name no longer exists and also for any variables dedicated to each application

For example, having the ROOTPATH constant pointing to the root folder which contains app, vendor, website1 and website2, while the other files (which are normally also found in the root), are instead contained in the various folders of the different applications, this could it cause problems in the functioning of anything?

Will preload.php, builds and other files included in the document roots (website1 and website2) continue to do their job in this location?

Am I doing something right or am I on the wrong track?
Reply
#3

(This post was last modified: 10-14-2023, 07:28 AM by Aakriti.)

Hi guys
Managing numerous programs within a single app folder with distinct configurations is a brilliant method that streamlines organization and reduces repetition. An efficient solution.GB whatsapp Apk
Reply
#4

You will also need to set the path in spark.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB