Moving the vendor directory for multiple projects

This How-To is useful if you are planning to move the Yii vendor directory outside the default location.

Or if there is a condition to develop another application under the same Yii2 framework, we could use the same vendor. It could save some space in the shared hosting.

Here we go:
  1. assume the root dir is '/var/www/html',
  2. the vendor dir is moved to '/var/www/html/frameworks/yii2',
  3. the application is located in '/var/www/html/myapp' without vendor directory as it's been moved (point 2) & should be accessible through http://hostname/myapp/web/.
To make the myapp to work, modify these files:
  1. the /web/index.php and yii (right under the project directory -- for Yii command from CLI). change the two lines of required file path (autoload.php & yii.php) to the correct vendor location.
  2. the /config/web.php. add vendorPath to the config with your vendor directory.
$config = [
    // if your framework vendor directory is moved to another place 
    // which is not standard, then set the vendorPath attribute to 
    // the new vendor directory.
    'vendorPath' => '../../frameworks/yii2',
    'components' => [...]
    // ... other configs
]
Composer

As the vendor is moved from /var/www/html/myapp/vendor to /var/www/html/frameworks/yii2, you will find problem installing a new extension or updating the existing through composer command. To fix it, modify your composer.json (right under your project directory) by adding the vendor-dir attribute under the config section, eg:

// other settings ...
"config": {
    "vendor-dir":"/var/www/html/frameworks/yii2",
    "process-timeout": 1800
},
// other settings ...

Also, you may find "a man-in-the-middle attack" message when either update or install the new extension (i don't know if there is a relation to the modification of composer.json but i get it several times during experiments). To fix it run composer update --dry-run.

Done!