Welcome Guest, Not a member yet? Register   Sign In
Shared Models between multiple applications
#1

I have multiple applications running on a single installation of CI. These apps share the same database.
Project Structure
/app1
    app
    public
/app2
    app
    public
/vendor
but i want these applications to have a shared model. A Model that can be accessed in app1 and app2 so i dont have to repeat the same model.
How do i achieve this if possible?
Reply
#2

Create new folder "/shared" on level "/vendor"
Create new files models as shared/BigModel.php, class BigModel with another namespace Shared\BigModel
Edit autoload config https://codeigniter.com/user_guide/conce...figuration added in array "Shared => ./shared"
Reply
#3

(This post was last modified: 09-25-2022, 11:35 PM by vimkaf.)

This is what i have tried
created a shared folder in the rootpath
shared
   \Models
       \OrderModel.php
   \Libraries
...

The contents of OrderModel.php

PHP Code:
<?php

namespace Shared\Models;

use 
CodeIgniter\Model;

class 
OrderModel extends Model
{
...... 
In my Autoload Config file I have

PHP Code:
  
public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
        'Config'      => APPPATH 'Config',
        'Shared' => ROOTPATH 'shared',
    ]; 


But it is still not working.
Reply
#4

I finally got this to work.
What I did was to create a SHARED_PATH constant in Common.php
PHP Code:
$d explode(DIRECTORY_SEPARATOR,ROOTPATH);
$d[count($d) -2] = "shared";

define('SHARED_PATH' implode(DIRECTORY_SEPARATOR$d)); 

Then use the SHARED_PATH in my Config/Autoload.php

PHP Code:
public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
        'Config'      => APPPATH 'Config',
        'Shared' =>  SHARED_PATH
]; 

Then in the my Controller I simply use the shared model

PHP Code:
use Shared\Models\OrderModel

OR

PHP Code:
$orderModel = new \Shared\Models\OrderModel(); 

Hope this helps someone.
Reply
#5

Your just creating a Module.
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