Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One shared vendor with two projects

I'm working on 2 applications right now. The first one is a CMS, and the second is a shop. I want to move my vendor one level above and the share it between projects.

So my structure will be something like this:

project1/
project2/
shared_vendor/

I read about this. I have changed the app/autoload.php loader variable from:

$loader = require __DIR__.'/../vendor/autoload.php';

to:

$loader = require __DIR__.'/../../vendor/autoload.php';

And I have also changed vendor-dir in my composer.json from:

    "config": {
      "bin-dir": "bin",
      "vendor-dir": "vendor"
    },

to:

    "config": {
      "bin-dir": "bin",
      "vendor-dir": "/../vendor"
    },

And after this I'm getting this error:

ClassNotFoundException in AppKernel.php line 20: Attempted to load 
class "CmsUserBundle" from namespace "Cms\UserBundle".
Did you forget a "use" statement for another namespace?

What am I doing wrong? Did I forget to change something? Thanks in advance.

like image 934
r3m4k3 Avatar asked May 02 '15 08:05

r3m4k3


1 Answers

Composer works on a per project basis.

One project - one vendor folder. Not, two projects and one "shared" vendor folder.

We had the "shared" vendor folder approach with PEAR long enough and it simply didn't work out. Managing different project requirements with a global vendor folder is a pain, because every project has different requirements.

Anyway...

if you like the "shared vendor folder" setup, i would suggest to create something like a "wrapper" or "super" project, which acts as container repository for the two other projects. The wrapper project will contain the composer.json file with the requirements for both(!) projects. That means that you are working against the same set of dependencies in both sub-projects.

This allows to define requirements for both sub-projects (cms and shop) in the "wrapper" repo. Basically, i'm suggesting the following structure:

|-container-project
  +-CMS
    |-src
    +-tests
  +-Shop
    |-src
    +-tests
  +-vendors      // contains dependencies for both projects (CMS + Shop)
|-composer.json  // define requirements for both projects

This setup allows to introduce composer.json files for the subprojects, too. You just have to transfer the requirements from the composer.json file of the super-project to the composer.json file of a subproject.

Now, it's also possible to tweak the autoloading behavior of the sub-projects by registering autoloaders in a specific order.

like image 92
Jens A. Koch Avatar answered Sep 28 '22 09:09

Jens A. Koch