2

Under the "Nesting Variables" section in Codeigniter4 site:

"To save on typing, you can reuse variables that you’ve already specified in the file by wrapping the variable name within ${...}" link to CI nesting Variables section

example in the documentation:

BASE_DIR="/var/webroot/project-root"

CACHE_DIR="${BASE_DIR}/cache"

TMP_DIR="${BASE_DIR}/tmp"

I was trying to use the following

app.baseURL = 'http://localhost:8080/'

google.redirect = ${app.baseURL}Google

However, it's assigning it as a literal when print_r($_ENV)

[google.redirect] => ${app.baseURL}Google

I've tried using non-namespaced keys including BASE_DIR (per the example) and it keeps printing as a literal.

What's strange - When I use the following:

CI_ENVIRONMENT = development

google.redirect = ${CI_ENVIRONMENT}Google

The result when print_r is:

[CI_ENVIRONMENT] => development

[google.redirect] => developmentGoogle

My question is - What am I doing incorrectly and/or how should these be set/used correctly?

According to the documentation, I should be able to use any key within the .env file that was already assigned using

${somekeyinthisfile}

dozor
  • 25
  • 1
  • 5
  • You are using it correctly. I've been having a poke around and it appears that the regex doesn't allow for . (dots). I'm still looking. – TimBrownlaw Jun 30 '20 at 18:37
  • Even without using the dots - I still can't get it to work sans CI_ENVIRONMENT. using x = test somerandomvariablename = foo It still reads as a literal: [google.redirect] => ${x}GoogleLogin [google.redirect] => ${somerandomvariablename}GoogleLogin – dozor Jun 30 '20 at 18:53
  • Ah ha so I was wondering why I wasn't getting the same thing... Now I've come up with a New Library file to override the existing system\config\DotEnv.php methods of load() and resolveNestedVariables(). So how much pain are you in with this not working at the moment? – TimBrownlaw Jun 30 '20 at 19:10
  • Not too much pain. I'm coming over from CI3 and was just playing around with CI4 to start the process of migrating my sites. If it's just waiting till there's a fix in the main code - I can wait. I'm not in any hurry. – dozor Jun 30 '20 at 19:24
  • Ok - Well I've got a 4am "patch" that makes it all sing and dance but it involves either altering the DotEnv.php core file directly, which in this case might not be a bad thing... The alternative is creating a new DotEnv.php that just overrides those methods... So the ole - change things without changing the system files directly. Both are fun :) So that was a fun little debug session :) – TimBrownlaw Jun 30 '20 at 19:27

1 Answers1

1

After a bit of looking, there is a more recent file up at https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Config/DotEnv.php with all the "other" changes...

This was a Bug Fix. So get that file and you will be good to go.

I am pretty sure that the intention wasn't to allow app.xxx settings to be used as variables as the documentation clearly shows, by not showing them being used. ( yes its 6am now ...)

BUT it is your code to do with as you please...So if you want to use app.xxx as variables...

The Only Thing missing is the DOT (.) in the regex

If you look on Line 272 - system/Config/DotEnv.php inside method resolveNestedVariables() and add a . (dot) into the regex, that will make all your app.things work.

  $value = preg_replace_callback(
     '/\${([a-zA-Z0-9_.]+)}/',
     function ($matchedPatterns) use ($loader) {

I have added a dot (.) at the end of the [a-zA-Z0-9_

So
'/\${([a-zA-Z0-9_]+)}/',

becomes

'/\${([a-zA-Z0-9_.]+)}/',
TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28