-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[11.x] Adding envOrFail()
helper
#52458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
As |
As I mentioned in my original post, I prefer not importing Since that is the only difference between |
envOrFail()
helper
You prefer not to import it on each config file, I prefer not to have on more user-defined function on the global namespace, regardless if I use it or not. But that is a matter of preference. Nonetheless, as this a new helper, added to the global namespace, I believe it should be sent to master, right? So it won't conflict in case someone already has a helper named the same in their codebase. |
The |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
Hey @taylorotwell I apologize for the late message; I was just out getting lunch and was talking to @jasonmccreary about this. He had actually suggested instead of a separate helper method, I add another (defaulted) argument to the existing The API we discussed was something like: If I was to submit a PR in that form, would it be more acceptable for you? |
Only if their code base is autoloaded by composer before the framework's. Otherwise, it will break. And even then, if the framework has a different implementation and this helper starts to be largely adopted by itself or by 3rd-party packages, then you end up with unexpected behavior. Just to be clear, I have a preference for not adding an extra helper with such a narrow use-case that can be easily solved by calling If this, or the alternate extra parameter to the As a workaround, you can either add this helper to your project's codebase, or pair the // this will fail with a missing API_KEY
throw_unless(env('API_KEY')); |
Oussama posted a tweet today about the
Env::getOrFail()
method, which allows a developer to ensure that if an environment variable is missing, the app will hard fail. This can be extremely useful in cases where the env var is an API key and you need to ensure that it's been set correctly, and based on the replies to the tweet, it seems like I'm not the only one who didn't know it exists but would find it helpful.I wanted to add this helper method so that I don't have to import the
Illuminate\Support\Env
class in every config file where I have API keys being loaded.Following the current convention, where
env()
actually proxies theEnv::get()
method, I createdenvOrFail()
which proxies theEnv::getOrFail()
method. I'm happy to rename this to something else if requested.If this PR is accepted, I will also add an entry in the docs publicizing this helper.