一个共享供应商有两个项目供应商、有两个、项目

2023-09-06 15:55:06 作者:清川带长薄

我现在正在处理 2 个应用程序.第一个是CMS,第二个是商店.我想将我的供应商提升一级并在项目之间共享.

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/

我读到了这个.我已将 app/autoload.php 加载程序变量从:

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 中的 vendor-dir 从:

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

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

到:

    "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.

推荐答案

Composer 在每个项目的基础上工作.

Composer works on a per project basis.

一个项目 - 一个供应商文件夹.不是,两个项目和一个共享"供应商文件夹.

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

我们使用 PEAR 的共享"供应商文件夹方法已经够久了,但它根本行不通.使用全局供应商文件夹管理不同的项目需求是一件很痛苦的事情,因为每个项目都有不同的需求.

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.

无论如何...

如果您喜欢共享供应商文件夹"设置,我建议创建类似于包装器"或超级"项目的东西,作为其他两个项目的容器存储库.包装器项目将包含 composer.json 文件,其中包含两个(!)项目的要求.这意味着您在两个子项目中使用相同的依赖项集.

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.

这允许定义两个子项目(cms 和 shop)的要求在包装器"回购中.基本上,我建议采用以下结构:

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

此设置也允许为子项目引入 composer.json 文件.您只需将需求从超级项目的 composer.json 文件转移到子项目的 composer.json 文件中.

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.