简体   繁体   English

当 Http 请求从另一个 Laravel 应用程序发送时,Laravel 使用不正确的数据库

[英]Laravel uses incorrect database when Http request is sent from another Laravel app

I seem to be running into a weird issue.我似乎遇到了一个奇怪的问题。 I am triggering a post request using the Http facade from a laravel app to another laravel app.我正在使用 Http 外观从 laravel 应用程序到另一个 laravel 应用程序触发发布请求。 Both these apps are linked to separate databases.这两个应用程序都链接到不同的数据库。 When I try to trigger the same endpoint using postman, it works fine but when the request is triggered from the other laravel app, the recipient laravel app tries to use the sender app's database settings which doesn't work.当我尝试使用 postman 触发相同的端点时,它工作正常,但是当从其他 laravel 应用程序触发请求时,收件人 laravel 应用程序尝试使用发件人应用程序的数据库设置。 I am currently using Xampp on Windows to host both these apps and the packages are the latest versions.我目前在 Windows 上使用 Xampp 来托管这两个应用程序,并且软件包是最新版本。 Has anyone experinced a similar issue or could you suggest a solution?有没有人遇到过类似的问题,或者您能提出解决方案吗?

The code is as follows: The service which sends the POST request (Sender App (1)):代码如下: 发送 POST 请求的服务(Sender App (1)):

Http::post("http://localhost/second_app/public/api/test", array(
                    'id' => 1,
);

The code which received the request (Receiver App (2)):收到请求的代码(Receiver App (2)):

public function test(Request $request)
{
    $club = Club::find($request->id);
}

I get an error in the log file which says that it is trying to find the clubs table in the first_app database while it should be using the second_app database.我在日志文件中收到一个错误,指出它正在尝试在first_app数据库中查找clubs表,而它应该使用second_app数据库。 I tried logging the configurations and the request.我尝试记录配置和请求。 The request is quite large to be posted here but I verified that is received correctly.该请求非常大,要在此处发布,但我确认已正确收到。 The code for this log is:此日志的代码是:

Log::info("Received Request", ['database' => ['driver' => config('database.default'), 'name' => config('database.connections.'.config('database.default').'.database')]]);

If the request is sent from Postman to second_app or from the second_app to itself (using the Http facade)如果请求是从 Postman 发送到 second_app 或从 second_app 发送到自身(使用 Http 门面)

[2021-08-17 03:13:56] local.INFO: Received Request {"database":{"driver":"mysql","name":"second_app"}} 

If the request is sent from first_app to second_app using the Http facade如果请求是使用 Http 门面从first_app发送到second_app

[2021-08-17 03:14:01] local.INFO: Received Request {"database":{"driver":"mysql","name":"first_app"}} 
[2021-08-17 03:14:01] local.INFO: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'first_app.projects' doesn't exist (SQL: select * from `projects` where `code` = ABC_01 limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'first_app.projects' doesn't exist (SQL: select * from `projects` where `code` = ABC_01 limit 1) at \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:692)

Config for the apps is left the same as default.应用程序的配置与默认设置相同。 The.env is filled with the following details .env 填充了以下详细信息

#first_app
DB_DATABASE="first_app"

#second_app
DB_DATABASE="second_app"

UPDATE I tried with separate vhosts as well.更新我也尝试使用单独的虚拟主机。 firstapp.test and secondapp.test was set up and the document root was pointed to the public directories. firstapp.testsecondapp.test已设置,文档根目录指向公共目录。 The issue remained the same and the incorrect configuration was used when the request was sent from first_app but it works correctly (as it did earlier) when the request is sent internally from the second_app or from Postman当从first_app发送请求时,问题仍然存在,并且使用了不正确的配置,但是当从second_app或从 Postman 内部发送请求时,它可以正常工作(就像之前所做的那样)

I tried hard-coding the database.php configuration values in the recipient app instead of using the env helper and that fixed the issue.我尝试在收件人应用程序中硬编码database.php配置值,而不是使用 env 帮助程序,这解决了问题。 It looks like the env helper was causing a conflict by using the sender app's environment variables in the recipient app.看起来 env 帮助程序通过在收件人应用程序中使用发件人应用程序的环境变量导致了冲突。

I got the same issue.我遇到了同样的问题。

and i fix it by caching all configuration using php artisan optimize .我通过使用php artisan optimize缓存所有配置来修复它。

hardcoding the database configuration also works硬编码数据库配置也有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM