Skip to content

Commit

Permalink
Clarify usage of load_dot_env() function
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Nov 29, 2024
1 parent 099c6c7 commit 5917306
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion doc/going-further/interacting-with-castor/dot-env.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ Example:
```php
use Castor\Attribute\AsTask;
use Castor\Context;

use function Castor\load_dot_env;

#[AsTask()]
function show_database_url(): void
{
$env = load_dot_env();

io()->writeln($env['DATABASE_URL']) ?? throw new \RuntimeException('DATABASE_URL is not defined');
io()->writeln($_SERVER['DATABASE_URL'] ?? throw new \RuntimeException('DATABASE_URL is not defined'));
}
```

> [!IMPORTANT]
> Even if the `load_dot_env()` function returns the env variables as an array,
> it will only return variables defined in the .env file. So if someone overrides
> one variable with true environment variable, it will not be returned. So it's
> better to use `$_SERVER` in you code to access env variables.
> [!NOTE]
> You can find more about how `.env` file loading and overloading works on
> [related Symfony documentation](https://symfony.com/doc/current/configuration.html#configuring-environment-variables-in-env-files).
Expand All @@ -40,6 +47,7 @@ You can also create a context that load a `.env` file:
```php
use Castor\Attribute\AsContext;
use Castor\Context;

use function Castor\load_dot_env;

#[AsContext()]
Expand Down

0 comments on commit 5917306

Please sign in to comment.