How to use the same vendor dirctory for multiple Yii 2 applications on shared hosting

Hello,

We have hosted or Yii 2 applications on shared hosting server. Both of the Yii 2 applications has vendor folder that contains same extensions. Now we want that the second application should access the first app’s vendor folder instead of having its own in order to optimize the disk space of the server. How to do this
The project structure is

folder 1
  - assets/
  - commands/
  - config/
  - controllers/
  - models/
  - views/
  - vendor/
  
folder 2
  - assets/
  - commands/
  - config/
  - controllers/
  - models/
  - views/
  - vendor/

public_html
     app1 --> contents from the web directory of app1 (Contains index.php)
     app2 --> contents from the web directory of app2  (Contains index.php)

Now the index.php from app1

require __DIR__ . '/../../folder1/vendor/autoload.php';
require __DIR__ . '/../../folder1/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../folder1/config/web.php';

Now the index.php from app2

require __DIR__ . '/../../folder2/vendor/autoload.php';
require __DIR__ . '/../../folder2/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../folder2/config/web.php';

Where should I make changes in order that the app2 can use the vendor folder of app1 to reduce the disk space

You didn’t post folder structure and how the two are placed in the file system. Expect zero answer until you do!

This is how the folders are on the shared hosting server.

folder 1 contains all the directories and files except web directory. Similarly for folder 2

so on the file manager of the shared hosting server contains 3 things

folder 1
folder 2
public_html -

  1. contains app1 folder that contains web directory files and folders of 1st project (folder 1)
  2. contains app2 folder that contains web directory files and folders of 2nd project (folder 2)

Which folder structure should I am supposed to post? Please can you tell

Can you put full paths of folder1, folder2 and vendor directories?

Please can you tell which paths, I am not getting what are you asking

app1 and app2 are the subdomains

These are all on live server not on my local server.

should I post the config/web.php file?

folder 1
  - assets/
  - commands/
  - config/
  - controllers/
  - models/
  - views/
  - vendor/

folder 2
  - assets/
  - commands/
  - config/
  - controllers/
  - models/
  - views/
  - vendor/

public_html
     app1 --> contents from the web directory of folder 1 (Contains index.php)
     app2 --> contents from the web directory of folder 2 (Contains index.php)

I mean something like
AppA: /www/appa/
AppB: /www/appb/
vendor: /www/appa/vendor

There is 1 folder public_html which is a web accessible folder

In my File Manager I have like this below which contains 3 folders

  1. folder 1 (contains all the files of project 1 other than web directory)
  2. folder 2 (contains all the files of project 2 other than web directory)
  3. public_html

So folder 1 contains

folder1/assets
folder1/commands
folder1/controllers/
folder1/models
folder1/views
folder1/vendor
folder1/widgets

Similarly folder2 contains

folder2/assets
folder2/commands
folder2/controllers/
folder2/models
folder2/views
folder2/vendor
folder2/widgets

Now in public_html folder I have the following structure

public_html
      app1
            - assets
            - css
            - js
            - plugins
            - index.php  
      app2
            - assets
            - css
            - js
            - plugins
            - index.php

Now the index.php of app1 contains the following

require __DIR__ . '/../../folder1/vendor/autoload.php';
require __DIR__ . '/../../folder1/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../folder1/config/web.php';

Now the index.php of app2 contains the following

require __DIR__ . '/../../folder2/vendor/autoload.php';
require __DIR__ . '/../../folder2/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../folder2/config/web.php';

It should work. If it is not, it is because require folder hierarchy is not correct. try putting absolute path to require instead of relative one

It is working. That is not the problem

I want that the second application should use the vendor of the first application. So that instead of uploading the vendor folder again for the second application which would waste the disk space of the server unnecessary.

So where should I make changes so that the second application should use the vendor directory of the first application

So if I make the changes to the index.php of app2 like below, it does not work. it gives error


require __DIR__ . '/../../folder1/vendor/autoload.php';
require __DIR__ . '/../../folder1/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../folder1/config/web.php';

Check this:
https://forum.yiiframework.com/t/one-yii-for-several-sites-is-it-possible/86116/7?u=tri

vendorPath is used internally.in yii2/base/Application.php

1 Like

Ok I will try…

Yes. it works.

Thank you…