Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel .env conflicts and overwrites between 2 laravel projects #19454

Closed
faiwiz opened this issue Jun 2, 2017 · 13 comments
Closed

Laravel .env conflicts and overwrites between 2 laravel projects #19454

faiwiz opened this issue Jun 2, 2017 · 13 comments

Comments

@faiwiz
Copy link

faiwiz commented Jun 2, 2017

  • PHP Version:5.6.21

I have two laravel projects Project-A for MSSQL database and Project-B for MSSQL Database
A-Porject laravel version 5.4.24
B-Porject laravel version 5.4.23

A-Porject is working for frontend ui and B-Porject some more tables are available mssql(client requirements) so I have to get Order from Project-A and Get Detail of Order History from Project-B

Database config in A-Porject

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mysql_database
DB_USERNAME=root
DB_PASSWORD=
DB_PREFIX=prefix_

Database config in B-Porject

DB_CONNECTION=sqlsrv
DB_HOST=mssqldatabase.sqlurl.compute.amazonaws.com
DB_PORT=1433
DB_DATABASE=DYNAX09_DATABASE
DB_USERNAME=USERNAME
DB_PASSWORD=PASSWORD
DB_PREFIX=db.

Code for get/orderDetails/ in Project-B

Routes/Api.php

Route::get('/get/orderDetails/{orderID}','Orders\OrderController@getOrderDetails');

so I am calling it from Model of Project-A
using library //ixudra/curl
$response = Curl::to(env('MSSQLAPILINK').'get/orderDetails/'.$orderID)->get();
This url works fine when I open it in browser but when getting records in Project-A and using Curl::to(env('MSSQLAPILINK').'get/orderDetails/'.$orderID)->get() Project-B will configured with Project-A's env configuration.

Note: when open in browser env('MSSQLAPILINK').'get/orderDetails/1' and print $_SERVER Project-B its showing correct configured information in

[DB_CONNECTION] => sqlsrv
[DB_HOST] => mssqldatabase.sqlurl.compute.amazonaws.com
[DB_PORT] => 1433
[DB_DATABASE] => DYNAX09_DATABASE
[DB_USERNAME] => USERNAME
[DB_PASSWORD] => PASSWORD
[DB_PREFIX] => db.

when I hit api service of orders (Project-A.local/api/getOrders) using POSTMAN and called Curl::to(env('MSSQLAPILINK').'get/orderDetails/'.$orderID)->get() from curl responses it loses [DB_*] in $_SERVER and using Project-A database and thrown following error

QueryException in Connection.php line 647:\n SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysql_database.orderDetails' doesn't exist (SQL: select * from ORDER_DETAILS where order_id = 1 limit 1)\n (I am wondering why its using mysql query format )

@themsaid
Copy link
Member

themsaid commented Jun 2, 2017

Sorry but this kind of questions should be asked on the forums, we keep this repo for bug reporting only. I suggest that you try to brief the issue a little bit and include only the details one might need to understand what might be wrong.

@themsaid themsaid closed this as completed Jun 2, 2017
@faiwiz
Copy link
Author

faiwiz commented Jun 2, 2017

This is core bug, i don't know where I post it
$_SERVER variables conflicts

I have just make a hack in core file vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
function env($key, $default = null)
{

    switch($key){
        case "DB_CONNECTION":
            return "mssqldatabase.sqlurl.compute.amazonaws.com";
        break;
        case "DB_HOST":
            return "sqlsrv";
        break;
        case "DB_PORT":
            return 1433;
        break;
        case "DB_DATABASE":
            return "DYNAX09_DATABASE";
        break;
        case "DB_USERNAME":
            return "USERNAME";
        break;
        case "DB_PASSWORD":
            return "PASSWORD";
        break;
        case "DB_PREFIX":
            return "db.";
        break;
    }

@themsaid
Copy link
Member

themsaid commented Jun 2, 2017

How is it a core bug? Can you share more details about your findings?

@themsaid
Copy link
Member

themsaid commented Jun 2, 2017

One thing I'd like to share, don't use env() in your code because it might return cached results, always use configuration options.

@sisve
Copy link
Contributor

sisve commented Jun 3, 2017

To clear things up; this is a known issue. It's just not documented.

You must run php artisan config:cache in both Laravel projects to generate a cached configuration. This is a requirement for Apache on Windows (and many other setups).

Dotenv will set process-wide environment variables, and only if they don't already exists. This means that the first Laravel app is executed, sets the first environment variables, then it calls the same webserver (in your case) where the second Laravel app sees that the environment variables are already set, and wont set the correct ones.

This often shows up when you have an application calling an api at the same machine, or during load tests. You should be able to open two browsers, point them to /projectA and /projectB, and reproduce this by just refreshing the page. A sleep(...) helps to slow things down.

Also, env() never returns cached results. It returns the currently set environment variable. When you use php artisan config:cache dotenv isn't loaded (Laravel 5.2+) and the environment variables aren't set. That's why you should use config() instead.

Ref: vlucas/phpdotenv#76 (comment)
Ref: #13906

@marwan2
Copy link

marwan2 commented Jan 31, 2018

This is a bug sir, not a forum discussion

@neoplomero
Copy link

This is BIG BUG, But thanks a lot @sisve !!! you saved my day very bad.

@walrt
Copy link

walrt commented Aug 7, 2018

I had this error in lumen, and i could not find how to run php artisan config:cache like explained by @sisve. Anybody know how to fix it in lumen? Thank's a lot!

@sisve
Copy link
Contributor

sisve commented Aug 7, 2018

@walber-teixeira For Lumen, use another webserver configuration (avoid Apache on Windows, for example), or write your configuration values directly in the config files (and stop using the .env file).

@vishal-adhikari
Copy link

I was having this same error and tried to clear each and everything to clear cache & config but nothing worked for me but php artisan config:cache worked. Thanks sisve.

@qwertynik
Copy link

@sisve
Thanks for adding clarity. It's 2020 and this 'bug' (or undesirable feature) still sucked a good amount of time 😂.
Any idea which setup on Windows is not prone to this?

@qwertynik
Copy link

Thanks for asking @faiwiz

@mauriciomueller
Copy link

Laravel is getting the same .env file of the main project, so it overwrites the variables of the second one. To fix you can run the project in another port or use prefix in your variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants