Skip to content

Use env() helper by invoking Dotenv\Dotenv directly#191

Open
paxperscientiam wants to merge 2 commits into
mattstauffer:masterfrom
paxperscientiam:Environment
Open

Use env() helper by invoking Dotenv\Dotenv directly#191
paxperscientiam wants to merge 2 commits into
mattstauffer:masterfrom
paxperscientiam:Environment

Conversation

@paxperscientiam

Copy link
Copy Markdown

Hey

I've provided an example of how to use the env() helper in conjunction with direct leverage of the Dotenv\Dotenv library. Unfortunately, the values aren't accessible with Illuminate\Support\Env; not sure what the missing bit is.

That said, maybe it's not important. IIRC, laravel folks say to only rely on app environment variables in the config files.

Thoughts?

@mattstauffer mattstauffer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick note about a typo.

In terms of this general idea, I would think this is an OK way to do it, although it would be worth a later PR that also showed how to hook that into the application like happens in Laravel? If you can't do that, could you at least create an issue for it? But once you fix this typo this is good to merge--thanks so much!

Comment thread components/support/index.php Outdated
Co-authored-by: Matt Stauffer <mattstauffer@users.noreply.github.com>
@paxperscientiam

paxperscientiam commented Aug 30, 2021

Copy link
Copy Markdown
Author

It appears that in the full installation of Laravel DotEnv/DotEnv is implemented here: https://github.com/laravel/framework/blob/9ed095dc2d0c254e6558b514ea285b767407034b/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L20

Unfortunately, Illuminate/Foundation standalone package is deprecated and already not compatible with latest version of at least one other standalone package (can't recall which atm).

"... although it would be worth a later PR that also showed how to hook that into the application like happens in Laravel"

So, I could definitely try this, but where should it go?

One thing that perplexes me, and this is probably related to laravel's boot code and not using Illuminate\Foundation\Application (sadly not available), is that I can access the .env variables with the Illuminate\Support\Env class, but they do not appear in return value of Config::getEnvironment().

Any ideas on how to address this?

code:
Screen Shot 2021-08-30 at 12 30 34 PM

output:
Screen Shot 2021-08-30 at 12 32 50 PM

EDIT:
With regard to line 4 of my code snippet (Config::setFacadeApplication($capsule->getContainer());), this was necessary to set facade root. I guess it worked because Application extends Container (?).

I'm gonna check if your Slim Application can be used to do likewise...brb

EDIT2:
Ok, this doesn't work.
Screen Shot 2021-08-30 at 12 42 59 PM

Fatal error: Uncaught RuntimeException: A facade root has not been set. in /Users/ramos/repos/Torch/components/support/vendor/illuminate/support/Facades/Facade.php:258 Stack trace: #0 /Users/ramos/repos/Torch/components/support/index.php(31): Illuminate\Support\Facades\Facade::__callStatic('getEnvironment', Array) #1 {main} thrown in /Users/ramos/repos/Torch/components/support/vendor/illuminate/support/Facades/Facade.php on line 258

EDIT3: @mattstauffer , lemme know if you think it would be better to create a new issue based on the above text.

EDIT4: hold off on merging. got something figured out.

EDIT5: see issue #192

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

Successfully merging this pull request may close these issues.

2 participants