WAMP Laravel - Sending API requests from one local site to another mixes up environment variables

自作多情 提交于 2020-04-05 05:57:08

问题


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 has APP_URL=http://a.local
  • Site B has APP_URL=http://b.local
  • I send a GET Request (using Guzzle) from Site A code to http://b.local/test
  • The /test endpoing in Site B simply dumps out dump(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!