Skip to main content

App Singleton

After boot, MaplePHP\Core\App is available as a singleton. Use it to resolve well-known directory paths and read the current environment.

Directory paths

use MaplePHP\Core\App;

$app = App::get();

$app->dir()->root(); // /path/to/my-app
$app->dir()->public(); // /path/to/my-app/public
$app->dir()->configs(); // /path/to/my-app/configs
$app->dir()->app(); // /path/to/my-app/app
$app->dir()->logs(); // /path/to/my-app/storage/logs
$app->dir()->cache(); // /path/to/my-app/storage/cache
$app->dir()->migrations(); // /path/to/my-app/database/migrations

Use these methods instead of hardcoding paths. They remain correct regardless of where the project is deployed.

Environment

$app->env();       // 'PROD', 'DEV', 'STAGE', 'TEST', etc.
$app->isProd(); // bool
$app->isDev(); // bool

The environment value comes from the env key in configs/app.php, which typically reads APP_ENV from .env.

Config access

Read configuration values from the App singleton without manually loading config files.

// All values from configs/app.php as an array
$app->app();

// A single value from configs/app.php
$app->getApp('timezone');

// All values from all files in configs/ as a merged array
$app->configs();

Usage in services

App::get() is available anywhere after the kernel boots. A common pattern is using it inside service constructors or provider boot() methods:

use MaplePHP\Cache\Cache;
use MaplePHP\Cache\Handlers\FileSystemHandler;
use MaplePHP\Core\App;

$cache = new Cache(
new FileSystemHandler(App::get()->dir()->cache())
);