Garet
1 week ago
107
5
Laravel

When to use config variable vs accessing env variable directly

Posted 1 week ago by Garet

I understand how both environment and configuration variables work (and can work together).

What I'm unsure of is when it's OK to access an environment variable directly.

Here's an example: let's say I want to restrict my application to a single IP address which might be different between development and production environments, so in my .env file I have:

RESTRICTED_IP=202.24.84.101

In my middleware I can then check for the IP address using:

if (env('RESTRICTED_IP'))

Alternatively in config\app.php I could create a variable like so:

'restricted_id' => env('RESTRICTED_IP', '')

Then in my middleware I would check for the IP using:

if (config('restricted_ip'))

Is the addition of the variable in the configuraton file superflous and should I just access the enviroment variable directly?

Please sign in or create an account to participate in this conversation.