CodeIgniter Forums
Share models,libraries,helpers multi applications - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Share models,libraries,helpers multi applications (/showthread.php?tid=78261)



Share models,libraries,helpers multi applications - piieerre - 12-24-2020

Good morning all

I am in the process of migrating all my applications from CI 3 to CI 4.
However, I encounter a small difficulty.
Indeed, on some projects, I have several applications on the same CI instance. I saw that on CI 4 this was also possible. However, I am wondering how to share models, libraries or helpers between these applications.
On CI 3, I put a shared folder at the root containing models / library / helpers and I loaded this folder in the autoload.
Example of structure on CI 3 :
apps
    frontfoffice
        config
        controller
        ...
    backoffice
        config
        controller
        ...

shared
    helpers
    models
    libraries
system
index.php

I can't get the same thing on CI 4. For example, I would like to share the same model between a backoffice and a frontoffice.
Does anyone have any ideas for me to solve this problem?

Thank you in advance

Pierre

Sorry for my english, I'm frenchWink


RE: Share models,libraries,helpers multi applications - InsiteFX - 12-24-2020

Namespaces

namespace yourName/Shared;

Then you:

use yourName/Shared/helperName etc;


RE: Share models,libraries,helpers multi applications - piieerre - 12-24-2020

Thank you for your reply
Sorry I do not really understand do.
I tried to do this but it doesn't work:

namespace App\Controllers;

class Home extends BaseController
{
public function index()
{
$userModel = new \shared\Models\TaskModel();

}

//--------------------------------------------------------------------
}


RE: Share models,libraries,helpers multi applications - InsiteFX - 12-24-2020

Try adding just the use clause first if it still wont work add the psr4 namespaces below and try it.

Did not have time to test this.

PHP Code:
<?php

namespace App\Controllers;

use 
shared\Models\TaskModel;

class 
Home extends BaseController
{
    public function index()
    {
        $userModel = new \shared\Models\TaskModel();
    }

    //--------------------------------------------------------------------
}

// You may need to also add them to the app/Config/Autoload.php.

public $psr4 = [
    'shared\helpers'   => ROOT 'shared/helpers',
    'shared\models'    => ROOT 'shared/models',
    'shared\libraries' => ROOT 'shared/libraries',
]; 



RE: Share models,libraries,helpers multi applications - guilliano - 08-08-2022

Hi,
This is not working in CI 4?
I have 2 apps. 
My case:

Dashboard
    App
        config
        controller
       Models
       ....



Home

    App
        config
        controller
        Models
       ....


Vendor


But how can I get the models of "home" in "Dashboard"?