Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

php - 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 "CmsUserBundle".
Did you forget a "use" statement for another namespace?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...