Yii 2.0: Moving the vendor directory for multiple projects

4 followers

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!

Total 2 comments

#18612 report it
Misbahul D Munir at 2014/12/01 11:04pm
Ask

What benefit? I still cannot use one vendor path for more than one application.

#18603 report it
ThePr0f3550r at 2014/11/28 03:42am
Use Yii 2 Advanced Template

You should use Yii 2 Advanced Template, its very simple

Leave a comment

Please to leave your comment.

Write new article