Documentation menu

Start here

Configuration and environments

Taphra reads its settings from YAML files in app/Config/ and picks the environment (development, staging or production) from the APP_ENV environment variable. This page covers the files, the modes, secrets, the settings that matter on a public server, and how configuration is cached.

The files

#
File What it configures Guide
app.yml the app: name, default controller, cache, sessions, logging, URLs, proxies, mail this page
routes.yml URL routes Routing
databases.yml database connections Database
migrations.yml where migrations live and how their state is kept Database
ai.yml AI providers and models AI
queue.yml queue connections Queues
cache.yml cache rules per controller action Cache
billing.yml payment providers Billing

A file that does not exist is not an error: every key in it returns its default.

Environment sections

#

Most files are split into sections. all applies everywhere; dev, stg and prod apply in one mode and override all key by key:

YAML
# app/Config/app.yml
app_name: my-app

all:
  default_controller: home
  default_action: index
  caching:
    method: file
  logging:
    method: file

dev:
  display_errors: 1

prod:
  display_errors: 0
  base_url: https://example.com
  trusted_proxies: [10.0.0.0/8]

routes.yml is the exception: its top-level sections are HTTP methods, and the router does not read dev, stg or prod there (Routing).

Modes and APP_ENV

#

The skeleton's app/bootstrap.php sets the mode on every request:

PHP
<?php
// app/bootstrap.php
$GLOBALS['APP_MODE'] = modeFromEnvironment(getenv('APP_ENV'));
APP_ENV Mode getMode()
dev, development, local development TPHR_DEVELOPMENT ('dev')
stg, stage, staging staging TPHR_STAGING ('stg')
anything else, or not set production TPHR_PRODUCTION ('prod')

A truthy IS_PRODUCTION environment variable forces production whatever APP_ENV says. Because an unset or mistyped APP_ENV means production, a server that forgot to set it never shows debug pages. php taphra serve sets APP_ENV=dev for its own server when the variable is not set.

The mode logic lives in Taphra\Framework, the framework's application singleton. Framework::mode() returns the current mode, Framework::modeFromEnvironment() maps an APP_ENV value, and the constants Framework::PRODUCTION, Framework::STAGING and Framework::DEVELOPMENT hold the same strings as the app's TPHR_PRODUCTION, TPHR_STAGING and TPHR_DEVELOPMENT. The global functions getMode() and modeFromEnvironment() call these methods, and the global class Taphra is an alias of Taphra\Framework.

PHP
use Taphra\Framework;

echo Framework::modeFromEnvironment('local'), "\n";     // dev
echo Framework::modeFromEnvironment('staging'), "\n";   // stg
echo Framework::modeFromEnvironment(false), "\n";       // prod (APP_ENV not set)
echo Framework::modeFromEnvironment('dev', '1'), "\n";  // prod (IS_PRODUCTION wins)

if (Framework::mode() === Framework::DEVELOPMENT) {   // or getMode() === TPHR_DEVELOPMENT
    echo "development mode\n";
}

Framework::getInstance() also gives the older service-locator style access (getConfigManager('app'), getRouter(), getCache(), getContainer()) that some framework code still uses; new code uses the classes directly or the service container.

What changes with the mode:

  • Development shows the Whoops debug page for uncaught errors, with the stack trace, request data and the queries the request ran. An HTTP exception keeps its status there (404 for a missing page, 403 for a failed CSRF check); other errors answer 500.
  • Staging and production answer with the right status and show the app's Views/error404.tpl.php (for a 404) or Views/error500.tpl.php (for any other error status) without details, and write the error to the log.
  • Production writes log entries from notice up; development writes everything (Logging).
  • url() refuses to build a URL from the request's Host header in production (see below).

Reading and setting values

#

Config::get() takes a dot-notation key: the part before the first dot names the file, the rest is the path inside it, after the all and current-mode sections are merged.

PHP
use Taphra\Config;

echo Config::get('app.app_name'), "\n";                  // skeleton
echo Config::get('app.caching.method'), "\n";            // file
echo Config::get('app.mail.from', 'noreply@localhost'), "\n";   // the default: no such key

$caching = Config::get('app.caching');                   // ['method' => 'file']
$app     = Config::get('app');                           // the whole merged file

Config::set('app.feature.beta', true);                   // in memory, for this request only
var_dump(config('app.feature.beta'));                    // bool(true)

Config::get() returns an empty string for a missing key when you give no default; the config($key, $default = null) helper returns null. Config::set() changes the value in memory only (handy in tests and in bootstrap.php); it never writes the YAML file.

Secrets and environment variables

#

Keep secrets out of the YAML files you commit. Taphra reads environment variables with getenv().

  • composer create-project and php taphra key:generate write a random 64-character APP_SECRET_KEY to .env (which the skeleton's .gitignore leaves out of Git). On a server, set that key and your other secrets in the server environment: a PHP-FPM pool (env[APP_SECRET_KEY] = ...), a systemd unit (Environment=), or a container (docker run -e, environment: in Compose).
  • .env holds one NAME=value per line; a value in double quotes may use \n, \t, \" and \\ and may span lines, one in single quotes is taken as written. A quote that is never closed stops the boot with an error that names the line, instead of swallowing every line after it.
  • Read a secret where you need it, for example Crypt::encrypt($data, getenv('APP_SECRET_KEY')) (Authentication and security).
  • ai.yml and billing.yml replace ${NAME} placeholders with environment variables, so api_key: "${ANTHROPIC_API_KEY}" keeps the key out of the file. Other files do not resolve placeholders.

Base URL and proxies

#

Two app.yml settings matter as soon as the app is on a public server:

YAML
# app/Config/app.yml
prod:
  base_url: https://example.com
  trusted_proxies: [10.0.0.0/8, 127.0.0.1]
  • base_url is what url() and asset() build absolute links from. Outside production, without it, url() falls back to the request's Host header. In production url() throws a Taphra\Exceptions\ConfigurationException when base_url is missing, because the Host header comes from the client and a forged one would end up in links such as password-reset emails. asset() uses app.asset_url (a CDN prefix, for example) when set.
  • trusted_proxies lists the addresses (single IPs or CIDR ranges, IPv4 or IPv6) of your load balancer or reverse proxy. Only when a request comes from one of them does Taphra read X-Forwarded-For for the client IP (Request::clientIP(), used by rate limiting and logs) and X-Forwarded-Proto to tell that the request was HTTPS (secure cookies, HSTS). Without it these headers are ignored, so a client cannot fake its address.
PHP
use Taphra\Config;

Config::set('app.base_url', 'https://example.com');
echo url('/posts/42'), "\n";        // https://example.com/posts/42
echo url(), "\n";                   // https://example.com
echo asset('css/app.css'), "\n";    // https://example.com/css/app.css

Drivers

#

Cache, sessions, logs and locks use files under app/tmp/ until you choose another driver in app.yml:

Setting Values Default
caching.method file, redis, memcache, apc (APCu); servers in caching.servers file
session.method file, database file
logging.method file, syslog, null (discard) file
logging.level debug, message, notice, warning, error notice in production, everything in development
lock.method file, database file

The Cache, Logging and Authentication guides cover each one; session cookie settings (session.cookie) are in Authentication and security. app/tmp/ must be writable by the web server user.

How configuration is cached

#

Parsing YAML on every request would be slow, so each file is compiled to a PHP array under app/tmp/config_cache/, which OPcache keeps in memory.

  • A changed YAML file is picked up on the next request, in every mode: each cached copy (in app/tmp/config_cache/ and, in staging and production, in the app cache) records the file's modification time and size and is used only while the file still has them.
  • php taphra cache:clear and php taphra optimize:clear delete both copies.
  • php taphra optimize compiles configuration and routes into one file loaded at boot; run it on every deploy and php taphra optimize:clear to undo it (Deployment).