Skip to content
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

[10.x] Introduce requireEnv helper #48261

Merged
merged 2 commits into from Sep 1, 2023
Merged

[10.x] Introduce requireEnv helper #48261

merged 2 commits into from Sep 1, 2023

Conversation

lucasmichot
Copy link
Contributor

It sometimes happens that we want to ensure that some environment variables are set, without relying on their default value, in case they are not found.

This is very much the case when working as a team with new joiners when setting up their Laravel project locally.

I believe it is valuable to have a way to defined if a given environment variable needs to be explicitly set.

This pull-request introduces the requireEnv() helper and the
\Illuminate\Support\Env::getRequired() method for this purpose.

They don't accept any default value, and an exception is thrown if the environment variable is not found.


Note:

The helper could also be named requiredEnv() (with a d), I don't have a strong opinion about this.

@lucasmichot
Copy link
Contributor Author

The cheksuite is 🔴 because of some flaky tests:
cf. https://github.com/laravel/framework/actions/runs/6037421961/job/16381669035?pr=48261#step:9:171

@rojtjo
Copy link
Contributor

rojtjo commented Aug 31, 2023

I really like this idea!

Only note I have is that requireEnv should be renamed to require_env to match the other framework helpers.

@taylorotwell taylorotwell merged commit f767da1 into laravel:10.x Sep 1, 2023
19 checks passed
@ankurk91
Copy link
Contributor

ankurk91 commented Sep 2, 2023

I don't see any global function introduced in this PR.
The commit message is confusing.
The commit message will become changelog in future.

@dennisprudlo
Copy link
Contributor

@ankurk91 Taylor removed the global helper in his commit before merging: 10f3b13

The commit "Introduce requireEnv helper" does indeed adds a global function: 7b7dd6b

@hamidafghan
Copy link

@taylorotwell or @lucasmichot can you explain how it will be used with an example. Thanks

@rojtjo
Copy link
Contributor

rojtjo commented Sep 4, 2023

@hamidafghan

<?php

use Illuminate\Support\Env;

// config/app.php
return [
    'name' => Env::getOrFail('APP_NAME'),
];

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.

None yet

6 participants