2

In my application I'm going to define a config file having string that depends on other string that I would like to put in parameters. I explain better by an example

/config/specs.php contents

$var1 = 'blah01';
$var2 = 'blah02';


return [
'specs01' => "This is $var1",
'specs02' => "This is $var2"
...
]

this is what I've found so far... and actually looks like working.

But I'm not convinced too much by this approach: am I putting some dirt in Larevel's global scope?

I know there's the chance to address to env file using env('var01') function to have parameters inside my config array. But also I don't like to have too many key,value pairs in .env file...

What do you suggest?

And am I going to encounter problems with the global scope of the framework? (this is something I'm still investigating...)

Many thanks

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
koalaok
  • 5,075
  • 11
  • 47
  • 91

1 Answers1

2

I don't think there is a better way to do this. .env file is for configuration that varies by environment and I understand those values are not environment specific.

You are not going to have any problems by adding a few variables to the global scope as Laravel and all other packages that are written in object-oriented rarely use global variables.

If you are really worried about global variables conflicts, you can store all variables in an array and assign this array to a variable with some name you're sure won't be used anywhere, e.g.:

$variableWithSuperUniqueName = [
  'var1' => 'blah01',
  'var2' => 'blah02',
];

return [
  'specs01' => "This is {$variableWithSuperUniqueName['var1']}",
  'specs02' => "This is {$variableWithSuperUniqueName['var2']}",
  ...
];

This way you'll pollute global scope with only one variable and avoid conflicts by giving this variable some unique name.

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • Thanks a lot, I was already applying the array approach in order to lessen the dirt. :-) – koalaok Aug 12 '15 at 15:01
  • I'd like to share this... I'm seeing also that if I have two different config files in config folder, the global variables don't interfere to each other. $variableWithSuperUniqueName = [ 'var1' => 'blah01', 'var2' => blah02']; can be defined in both files with different keys, values and it will work as expected. Maybe variables defined here are not global, or the config() func loads the file with its "scope".... – koalaok Aug 12 '15 at 16:10