skip to Main Content

How to call models from other multi application site in single installation codeigniter 4 ?

The folder structure look like this :

- WebsiteFolder
  -- Site1
     --- app
     --- public
     --- tests
     --- writeable
     (.env, spark and other file)
  -- Site2
     --- app
     --- public
     --- tests
     --- writeable
     (.env, spark and other file)
  -- system

This is my example code :

In Site1


Constants.php
I have define a root directory to targeted the site2.

define('ROOTSOURCE', dirname(__DIR__,3) . 'site2');

This return :

E:Projectwebsitesite2

Autoload.php

I have setup PSR4.

 public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',        
        'SourceModels' => ROOTSOURCE . '/app/Models/'
    ];

Then I Run SPARK COMMAND :

php spark namespaces

And return

+---------------+-----------------------------------------------------------------------------------------+--------+
| Namespace     | Path                                                                                    | Found? |
+---------------+-----------------------------------------------------------------------------------------+--------+
| CodeIgniter   | E:ProjectDennisLiuwebsitesystem                                         | Yes    |
| App           | E:ProjectDennisLiuwebsitesite1app        | Yes    |
| Config        | E:ProjectDennisLiuwebsitesite1appConfig | Yes    |
| SourceModels | E:ProjectDennisLiuwebsitesite2appModels           | Yes    |
+---------------+-----------------------------------------------------------------------------------------+--------+

Then NameSpace SourceModels is Found. So far everything is okay.

Controller => Home.php

namespace AppControllers;
use SourceModels;


class Home extends BaseController
{
    public function index()
    {                
        
        $setting = new SourceModelsSetting();                                                    

        return view('welcome_message');
        
    }

When I run the controller I got :

Class "SourceModelsSetting" not found

Next,

In Site2

I have model "Setting" in Site2 Model Folder.

For Note :

Everything In Site 2 Is running properly.

My question is for the error I got "Class "SourceModelsSetting" not found" What is the proper setting to call the site 2 model in single application installation codeigniter 4 ?.

For Note :
This is single installation codeigniter 4 for two website. And I shared the system folder.

3

Answers


  1. Chosen as BEST ANSWER

    I found the problem. This is the correct way to do this.

    Folder Structure

    - WebsiteFolder
      -- Site1
         --- app
         --- public
         --- tests
         --- writeable
         (.env, spark and other file)
      -- Site2
         --- app
         --- public
         --- tests
         --- writeable
         (.env, spark and other file)
      -- shared/Models
         (DBSetting.php)
      -- system
    

    Controller - Home.php

    namespace AppControllers;
    use sharedModelsDBSetting;
    
    
    
    class Home extends BaseController
    {
        public function index()
        {                
            
            $db = new sharedModelsDBSetting();                
    
            return view('welcome_message');
            
        }
    }
    

    Autoload.php

     public $psr4 = [
            APP_NAMESPACE   => APPPATH, // For custom app namespace
            'Config'        => APPPATH . 'Config',               
            'sharedModels' => ROOTSOURCE . '/shared/Models'        
        ];
    

    Constants.php

    define('ROOTSOURCE', dirname(__DIR__,3));
    

    DBSetting.php

    namespace sharedModels; use CodeIgniterModel;

    class DBSetting extends Model {

    function __construct()
    {       
        parent::__construct();                  
    }
    
    
    public function save() {    
        return true;
    }   
    

    }

    We can also call the model in site 2. Just set the correct path in Autoload.php to refer to models in site 2.

    Note : If the model in site 2 content another model or link, if we call from the site 1 then codeigniter 4 system will read the link, model from site 1. So make sure to call the plain model in site 2. Or just create a share model folder like above.


  2. You can access models within your classes by creating a new instance or using the model() helper function.

    example like this

    // Create a new class manually
    $userModel = new AppModelsUserModel();
    
    // Create a new class with the model function
    $userModel = model('AppModelsUserModel', false);
    
    // Create a shared instance of the model
    $userModel = model('AppModelsUserModel');
    
    Login or Signup to reply.
  3. It makes no real sense to me for different apps call each others models. If this was a HMVC structure where you had modules and they could talk to each other inside the same app, that would make sense. But since these are two different apps makes no sense because in the long run you’re basically creating dependencies between them when they should work on their own.

    Either way, if you really want to do that you may need to do that in a native way and not in a codeigniter way.

    namespace AppControllers;
    require_once ROOTSOURCE . '/app/Models/UserModel.php';
    
    class Home extends BaseController
    {
        public function index()
        {                
            //Access the model like so:
            $setting = new UserModel();                                                    
    
            return view('welcome_message');
            
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search