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.
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:
For your app: public/index.php
require __DIR__.'/../../vendor/autoload.php';
Your artisan command in the root folder: artisan
require __DIR__.'/../vendor/autoload.php';
Package auto-discovery in Illuminate\Foundation\PackageManifest
:
$this->vendorPath = $basePath.'/../vendor'; //Change this line in constructor
and rerun
php artisan package:discover --ansi
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 ;)
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.
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.