Wednesday, March 8, 2017

Yii2 configuring to use vendor folder in an external http storage

Leave a Comment

I have multiple restful apis build using yii2 framework, What i wouldlike is to use a single vendor directory stored in a different address

That is

I have an application hosted at www.example1.com and another one hosted at www.example2.com and the one i would like to contain the vendor files to be at www.example3.com so that in both example1 and example2 i only have to upload the resful folder without vendor directories

After some looking into yii2 advanced folder i have found these lines in index.php

require(__DIR__ . '/_protected/vendor/autoload.php'); require(__DIR__ . '/_protected/vendor/yiisoft/yii2/Yii.php'); 

Ive tried as a work around by doing

require(__DIR__ . 'http:://example1.com/pathto/_protected/vendor/autoload.php'); 

But the above fails, How best can i achieve this

3 Answers

Answers 1

You can't do it via HTTP.

The only solution is to create shared, NFS volume, which will be mounted on both servers as your vendor/ directory. After you will be able to use the same vendor dir for two servers.

If those two sites are deployed to the same server, it might be easier.

Answers 2

It is very simple . you can just copy the vendor file into your next project and change the two lines of required file path (autoload.php & yii.php) to the correct vendor location. the /config/web.php. add vendorPath to the config with your vendor directory. 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":"your path", "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. then it would ok.

Answers 3

This can be done: Include through http. You need to set some php variables (allow_url_include, allow_url_fopen). And you need to get rid of __DIR__ constant in front of your php file path:

require('http://example1.com/pathto/_protected/vendor/autoload.php'); 

BUT

Don't do this. Really. You don't want to go through all these unsafe php setups. And you don't want to expose your vendor files to all the people in the world. If your script can access php source code via http, everyone can. Also, includes over http will be extremely slow.

The (probably) only solution

If you really want to share core php files between multiple websites, you can purchase virtual server, set up both websites on it and create one folder accessible from all websites on this server.

You don't even need virtual server, you can go with some kind of multihost service with custom folder for each website and shared folder for core (vendor) files.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment