Skip to content

Using .env files

The load_dot_env() function

You can load a .env file with the load_dot_env() function. This will:

  • load the .env file
  • populate the env variables for the current process
  • return the env variables as key/value array.

Note

By default, it loads the .env file on your project root (where castor file or folder was found), but you can overload this by passing your .env file path as an argument.

Example:

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'));
}

Note

You can find more about how .env file loading and overloading works on related Symfony documentation.

Create a context from a .env file

You can also create a context that load a .env file:

use Castor\Attribute\AsContext;
use Castor\Context;
use function Castor\load_dot_env;

#[AsContext()]
function my_context(): Context
{
    return new Context(load_dot_env());
}