问题
I am developing an API service that another site I've developed will be using. So locally when building and testing, obviously I want both local copies of the site to work. However, it seems to mix up the environment
variables.
For example:
Site A
hasAPP_URL=http://a.local
Site B
hasAPP_URL=http://b.local
- I send a
GET
Request (usingGuzzle
) fromSite A
code tohttp://b.local/test
- The
/test
endpoing inSite B
simply dumps outdump(env('APP_URL'))
- Result retrieved by
Site A
is"http://a.local"
- Expected result:
"http://b.local"
So the code in Site B
is running with environment variables loaded from Site A
. This is an issue as Site B
cannot access the correct database, it's trying to use the Site A
's database.
Is this an issue with my local setup (Win10 + WAMP), PHP settings, Laravel settings?
回答1:
I also encountered this issue, and it is mentioned here. The resolution for it is to run php artisan config:cache in both projects to cache configuration from .env files or patch the code from here.
回答2:
are you using artisan commands to run both projects with different ports ?
php artisan serve --port=8000
php artisan serve --port=8010
回答3:
You can set Environment variables in either the vhost config OR in an .htaccess
file:
SetEnv APP_URL http://b.local
回答4:
Apart from @Daniel Protopopov answer above there is also another way, that is also works when both Site A
and Site B
are Lumen.
In short just rename your DB_DATABASE
variable on each side to a different name. Then change the respective variable names in the respective config/<configfilename>.php
files.
So that on Site A
you would have SITE_A_DB_DATABASE
in .env
and matching 'database' => env('API_A_DB_DATABASE', 'forge'),
line in config/database.php
.
Then your Site B
SITE_B_DB_DATABASE
will not be overwritten due to variable names are different.
The same solution applies for any .env
variables which names match.
来源:https://stackoverflow.com/questions/51846513/wamp-laravel-sending-api-requests-from-one-local-site-to-another-mixes-up-envi