ImArtur
165
4
Laravel

Share classes between projects with composer does not work.

Posted 7 months ago by ImArtur

I'm creating a project. It will be pretty big admin application and client application. Both of those applications need to run separately. Those projects will share the same database and many modals, enumerations, transformers will be shared between each other. Structure is as follow:

system 
--admin
--client
--packages

I was thinking about creating packages folder to hold all my models so I can require it into each project. This is how I did it:

packages composer.json:

{
  "name": "system/db",
  "description": "db",
  "type": "library",
  "autoload": {
    "psr-4": {
      "System\Db\": "src"
    }
  }
}

I created Test class in packages:

<?php
namespace System\Db;
class Test
{
    public function hai(){
        echo 'Hi';
    }
}

After that I required it into my 'admin' project like this:

        "system/db": "*"

I run:

composer dump-autoload

and Tried to test it:

<?php
use System\Db\Test;

Route::get('/', function () {
    (new Test())->hai();
    dd('a;');
    return view('welcome');
});

I get a error:

"Class 'System\Db\Test' not found"

Any ideas why? Do you have better idea to share data between projects? Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.