r3m4k3
r3m4k3

Reputation: 182

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.

Upvotes: 6

Views: 4794

Answers (4)

flibidi
flibidi

Reputation: 11

I know this is an old question but I run into the same problem. I'm using Laravel 7 and I solved the problem with the following method.
I wrote a shellscript (my-vendor.sh) at the root of my project with the following code :

 #!/bin/bash

MY_VENDOR_DIRECTORY=my-vendor
VENDOR_DIRECTORY=vendor

if [ ! -d "$MY_VENDOR_DIRECTORY" ]
then
  echo "Le dossier $MY_VENDOR_DIRECTORY n'existe pas : on le crée."
  mkdir "$MY_VENDOR_DIRECTORY"
else
  echo "Le dossier $MY_VENDOR_DIRECTORY existe : on le vide."
  rm -rf "$MY_VENDOR_DIRECTORY/*"
fi

echo "Copie des fichiers autoload généré depuis $VENDOR_DIRECTORY vers $MY_VENDOR_DIRECTORY."
cp "$VENDOR_DIRECTORY/autoload.php" "$MY_VENDOR_DIRECTORY"
cp -R "$VENDOR_DIRECTORY/composer" "$MY_VENDOR_DIRECTORY"

cd "$MY_VENDOR_DIRECTORY/composer"

echo "Remplacement de 'require \$file' par \str_replace(\"$MY_VENDOR_DIRECTORY\/composer\/..\", \"vendor\", \$file)"
sed -i "s/require \$file/require \\str_replace('$MY_VENDOR_DIRECTORY\/composer\/..', 'vendor', \$file)/" autoload_real.php

echo "Remplacement de 'include \$file' par \str_replace(\"$MY_VENDOR_DIRECTORY\/composer\/..\", \"vendor\", \$file) "
sed -i "s/include \$file/require \\str_replace('$MY_VENDOR_DIRECTORY\/composer\/..', 'vendor', \$file)/" ClassLoader.php

Then, I replace the following code in public/index.php :

  • require __DIR__.'/../vendor/autoload.php' => by require __DIR__.'/../my-vendor/autoload.php';

After that, I add my-vendor.sh command in the post-autoload-dump scripts.

I made this modifications in my two projects, launch composer dump-autoload and it work like a charm :)

A link to the gist : https://gist.github.com/flibidi67/7c2cfdc1ff1b977b48204be0bee5eb76

Hope this can help someone else ;)

Upvotes: 1

BassMHL
BassMHL

Reputation: 9057

For Laravel 5, 6 and 7+

After adding the new vendor folder config:

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

Then run composer update

Then you need to change two files:

  1. For your app: public/index.php

    require __DIR__.'/../../vendor/autoload.php';

  2. Your artisan command in the root folder: artisan

    require __DIR__.'/../vendor/autoload.php';

  3. Package auto-discovery in Illuminate\Foundation\PackageManifest:

    $this->vendorPath = $basePath.'/../vendor'; //Change this line in constructor

and rerun

php artisan package:discover --ansi

Upvotes: 1

Abdou Tahiri
Abdou Tahiri

Reputation: 4408

Based on your requirements and if the only thing you need/want to share between your multiple projects is the vendor directory, you could just create symlinks in each project except whatever the main one which already has the the vendor directory.

For example:

cd /var/www/SiteA
composer install
# git clone new project into /var/www/SiteB
cd ../SiteB
# If vendor directory is already there, delete it: rm -rf vendor
# create symlink to Site A's vendor directory
ln -s /var/www/SiteA/vendor

Unless you know for sure that all projects are definitely going to need exactly the same versions of your dependancies, this maybe not a good plan.

Upvotes: 1

Jens A. Koch
Jens A. Koch

Reputation: 41776

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.

Upvotes: 8

Related Questions