6

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?

Giedrius
  • 1,370
  • 3
  • 14
  • 37
  • Any apache logs with errors? Or everything clear? – Tarasovych Aug 14 '18 at 19:54
  • Nope, everything seems clear. Everything does work indeed, except using wrong environment variables :/ – Giedrius Aug 14 '18 at 20:22
  • waht's about apache configs? I guess both sites have the same root path... – Tarasovych Aug 14 '18 at 20:28
  • Yes, they have the same root path of `C:/wamp/www`. I don't know if there is any Apache config that would be related to PHP loading the environment variables? – Giedrius Aug 14 '18 at 20:51
  • so you have to specify different root path for each app, don't you? E. g. `/wamp/www/app1` and `/wamp/www/app2`. You just load both sites with the same config. So you got actually one site) – Tarasovych Aug 14 '18 at 21:20
  • @Tarasovych sorry I must've misunderstood you then. Yes, each site has it's own directory within the room I've specified, so one is loaded from `www/sitea/public` and the other from `www/siteb/public` – Giedrius Aug 15 '18 at 00:47
  • 1
    Did you tried to `SetEnv env_var_name env_var_value` in each `VHost`? – mdh.heydari Aug 23 '18 at 20:14
  • Hi! Can you provide, please, your web server configuration for both applications? – YanDatsiuk Aug 27 '18 at 08:00
  • did you configure vhosts for b.local and a.local for different directories? – Erkan Özkök Aug 27 '18 at 09:26
  • @ErkanÖzkök yes they are different apps and they run fine in the browser (completely different and correct), it's only when sending API requests that it seems to load wrong environemnt variables. – Giedrius Aug 27 '18 at 22:51
  • @Giedrius it seems like WAMP problem (especially Apache+PHP). In dev although a little bit slower I prefer not to use cache at all. So everything should be working fine if you run `php artisan cache:clear` and `php artisan config:clear`. Unfortunately, I guess it will not work in your case due to issues mentioned by @Daniel Protopopov. So instead of patching your app (which I find as Bad Practice), I would use [Homestead](https://laravel.com/docs/5.6/homestead) since it provides environment as close as possible to production, and I haven't seen any problems with it (with cache enabled also). – Bart Aug 29 '18 at 22:26
  • did php artisan config:cache solved your problem – Aabir Hussain Aug 30 '18 at 11:46
  • Can you please show where have you declared these variables ? – Haris Mehmood Aug 30 '18 at 14:48
  • - If from terminal you do `curl http://b.local/test`, what do you get? - Does `Site A` have a `/test` endpoint as well? Are you sure that the endpoint called is correctly the one from `Site B`, with wrong environment variables? – gbalduzzi Aug 30 '18 at 16:16

6 Answers6

3

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.

Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39
0

are you using artisan commands to run both projects with different ports ?

php artisan serve --port=8000

php artisan serve --port=8010 
Meshal Alhazmi
  • 346
  • 1
  • 2
  • 11
  • No, I don't use `php artisan serve` at all, all the local sites are managed by `WAMP` http://www.wampserver.com/en/ with different `VHosts` within `Apache`. Simply navigating the sites locally via browser works fine, it's only when one local site sends a request to another local site the environment does not load. – Giedrius Aug 14 '18 at 17:37
0

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.

Valentine Shi
  • 6,604
  • 4
  • 46
  • 46
0

Because the command php artisan config:cache doesn't work here (closure needed in routes file config file)

LogicException  : Your configuration files are not serializable.

I add phpdotenv with composer :

composer require vlucas/phpdotenv

And at the begginning of the file "/bootstrap/app.php" (after "new Illuminate\Foundation\Application"), I add :

$app->detectEnvironment(function () {
    $dotenv = Dotenv\Dotenv::create(__DIR__ . '/../', '.env');
    $dotenv->overload();
});

Maybe an alternative

Dawwwid
  • 1
  • 1
  • 1
  • As far as I know, `config:cache` does not cache routes, `route:cache` does. You must have something like a closure in your configuration files? – Giedrius Jun 05 '20 at 15:01
  • Yes @Giedrius, the `spatie/laravel-permission` package passes a `DateInterval` to the cache and I have not yet found how to solve the problem : `'expiration_time' => \DateInterval::createFromDateString('24 hours'),` – Dawwwid Jun 07 '20 at 06:38
  • I corrected my previous message ;) thanks @Giedrius – Dawwwid Jun 07 '20 at 19:55
0

If you are calling a Lumen 8 API from within a Laravel 6 application using GuzzleHttp and the Laravel env is being inherited to Lumen, creating config file worked for me.

In bootstrap/app.php comment below lines to prevent loading current env values from Laravel

// (new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
//     dirname(__DIR__)
// ))->bootstrap();

In bootstrap/app.php add below line after $app has been created.

$app->configure('database');

Create config/database.php in lumen root folder. Return all env values needed for Lumen api in an array in the config file.

<?php
return [
'timezone' => 'UTC',
'default' => 'pdbmysql',
'connections' => [
    'pdbmysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'port'      => '3306',
        'database'  => 'db2',
        'username'  => 'root',
        'password'  => 'root',
    ],

],
];
SMJ
  • 716
  • 1
  • 9
  • 23
-1

You can set Environment variables in either the vhost config OR in an .htaccess file:

SetEnv APP_URL http://b.local
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39