找到你要的答案

Q:One shared vendor with two projects

Q:一个共享的供应商与两个项目

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.

我现在正在处理2个应用程序。第一个是CMS,第二个是商店。我想移动我的供应商一级以上的项目之间共享。

所以我的结构会是这样的:

project1/
project2/
shared_vendor/

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

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

以:

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

我也改变了我的composer.json从供应商目录:

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

以:

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

在此之后,我得到这个错误:

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.

answer1: 回答1:

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.

作曲家作品根据每个项目的基础。

一个项目-一个供应商文件夹。没有,两个项目和一个“共享”供应商文件夹。

我们有“共享”供应商文件夹的方法与梨足够长,它根本没有工作。管理不同的项目要求与全球供应商文件夹是一个痛苦,因为每个项目有不同的要求。

反正…

如果你喜欢“共享供应商文件夹”设置,我建议创建一些像“包装”或“超级”项目,作为容器储存库的其他两个项目。包装项目将包含双方的要求composer.json文件(!)项目.这意味着您正在对两个子项目中的同一组依赖项进行处理。

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.

现在,调整子项目的行政行为在一个特定的顺序,也可以注册自动装弹机。

php  symfony2  composer-php  autoload  vendor