Home  >  Q&A  >  body text

Call multiple application models from a shared site in a single installation of Codeigniter 4

<p>How to call models from other multi-application sites in a single installation of Codeigniter 4? </p> <p>The folder structure looks like this: </p> <pre class="brush:php;toolbar:false;">- WebsiteFolder --Site1 ---app ---public --- tests --- writeable (.env, spark and other files) --Site2 ---app ---public --- tests --- writeable (.env, spark and other files) -- system</pre> <p>Here is my sample code: </p> <p><strong>At site 1</strong></p> <hr /> <p><strong>Constants.php</strong> I have defined a root directory to locate site2. </p> <pre class="brush:php;toolbar:false;">define('ROOTSOURCE', dirname(__DIR__,3) . '\site2');</pre> <p>This returns: </p> <p><em>E:\Project\website\site2</em></p> <p><strong>Autoload.php</strong></p> <p>I have set up PSR4. </p> <pre class="brush:php;toolbar:false;">public $psr4 = [ APP_NAMESPACE => APPPATH, // For custom app namespace 'Config' => APPPATH . 'Config', 'Source\Models' => ROOTSOURCE . '/app/Models/' ];</pre> <p>Then I run the SPARK command: </p> <pre class="brush:php;toolbar:false;">php spark namespaces</pre> <p>and return</p> <pre class="brush:php;toolbar:false;"> --------------- ------------------ -------------------------------------------------- --------------------- -------- | Namespace | Path | Found? | --------------- ---------------------------------- -------------------------------------------------- ---- -------- | CodeIgniter | E:\Project\DennisLiu\website\system | Yes | | App | E:\Project\DennisLiu\website\site1\app | Yes | | Config | E:\Project\DennisLiu\website\site1\app\Config | Yes | | Source\Models | E:\Project\DennisLiu\website\site2\app\Models | Yes | --------------- ---------------------------------- -------------------------------------------------- ---- -------- </pre> <p>Then find the namespace <strong>Source\Models</strong>. So far so good.</p> <p>Controller=> <strong>Home.php</strong></p> <pre class="brush:php;toolbar:false;">namespace App\Controllers; use Source\Models; class Home extends BaseController { public function index() { $setting = new \Source\Models\Setting(); return view('welcome_message'); }</pre> <p>When I run the controller I get: </p> <blockquote> <p><strong>Class 'Source\Models\Setting' not found</strong></p> </blockquote> <p>Next step,</p> <p><strong>At site 2</strong></p> <p>I have the model<strong>Settings</strong> in the Site2 models folder. </p> <p><strong>Note:</strong></p> <p>Everything is working fine in Site 2. </p> <p>My problem is that I get the error "Class <strong>"Source\Models\Setting"" not found"</strong>The correct settings for calling site 2 models in a single application installation of codeigniter 4 are What? . < /p> <p><strong>Note: </strong> This is a single installation of codeigniter 4 for both websites. I shared the system folder. </p>
P粉653045807P粉653045807437 days ago486

reply all(2)I'll reply

  • P粉493534105

    P粉4935341052023-09-01 20:04:17

    You can access the model in a class by creating a new instance or using the model() helper function.

    Examples like this

    // Create a new class manually
    $userModel = new \App\Models\UserModel();
    
    // Create a new class with the model function
    $userModel = model('App\Models\UserModel', false);
    
    // Create a shared instance of the model
    $userModel = model('App\Models\UserModel');

    reply
    0
  • P粉722409996

    P粉7224099962023-09-01 10:37:48

    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 App\Controllers;
    use shared\Models\DBSetting;
    
    
    
    class Home extends BaseController
    {
        public function index()
        {                
            
            $db = new \shared\Models\DBSetting();                
    
            return view('welcome_message');
            
        }
    }

    Autoload.php

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

    Constants.php

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

    DBSetting.php

    Namespace Sharing\Model; Using CodeIgniter\Model;

    Class DBSetting extended 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 reference the model in site 2.

    Notice: If a model in site 2 contains another model or link, if we call from site 1, the codeigniter 4 system will read the link, model from site 1. So make sure to call the normal model in site 2. Or just create a shared model folder as above.

    reply
    0
  • Cancelreply