I am setting up multiple websites with CodeIgniter 4 that will share the same resources (IE: Models, Library, Helpers, ThirdParty, etc). My goal is to be able to manage code updates and migrations in the shared resources so the changes are global throughout all the websites. Below is an example of the structure:
How can I use the shared resource in WEBSITE_1 while it is outside of the directory?
Any help would be greatly appreciated!
Thank you in advance!!!
This is quite easy in CI4. Following is one of the approaches that you can take for achieving this.
Let's say the default setup is app -> CI4 folders.
Then, you will need to create another folder, let's say Website1 on the same level as your base app folder in CI4.
Once you do this, the Routes of the Website1 should be stored within the Website1 folder with the same folder structure as the original CI4 project.
Migrations will be within Website1/app/Migrations folder and if your connections are different then you will need to either mention the connection name on the migrations or switch them dynamically.
For the CI4 to find your websites routes successfully, you will need to mention the routes in Autoload file as follow:
$psr4 = [
'App' => APPPATH, // To ensure filters, etc still found,
APP_NAMESPACE => APPPATH, // For custom namespace
'Config' => APPPATH . 'Config',
'Website1' => ROOTPATH .'Website1',
'Website1/app' => ROOTPATH .'Website1/app'
This will enable you to use the classes defined within App or Website1 to be used anywhere in the CI4 project.
Example of Folder Structure :
Resources to be shared :
app\Config\ <-- For shared Configurations
app\Models\ <-- For shared Models
app\Database\Migrations\ <-- For shared Migrations
Website1 Folder:
Website1\app\Config\ <-- For Website1 Configurations
Website1\app\Models\ <-- For Website1 Models
Website1\app\Database\Migrations\ <-- For Website1 Migrations