Skip to content

Core\Config::setting

Returns a value your program reads from the [settings] block of nvs.toml, or null if no block sets it.

Use it for values that belong to your program, such as a page size or the address of an API. The value keeps its type from the file: a string, an int, a float, a bool or an array. A table such as [settings.api] is an array with the names as keys. The name api.base reads one key of it.

Use as to convert the value. as int throws an error when the value is missing or is not a whole number. as ?int gives null, and ?? then gives a default.

An [app.settings] block in an [[app]] block replaces values of the same name, for that application only.

Good to know: a setting is not tainted, because the person who runs the server wrote it. Your program cannot change a setting. Use a variable when one request needs another value. A TOML date is not allowed, so write it as a string.

Parameters

  • $namestringThe name of the value, such as page_size. A value in a table, such as [settings.api] with base = "...", has the name api.base.

Return value

Returns ?string|int|float|bool|array<mixed>. The value with the type it has in the file. A TOML string is a string, an integer is an int, a float is a float and a boolean is a bool. A TOML array is an array, and a table is an array with the names as keys. The result is null when no block sets the name. Use as to convert it: Core\Config::setting('page_size') as int throws an error when the value is missing or is not a whole number.

Examples

The examples below run with this configuration file.

nvs.toml
# The values these examples read with `Core\Config::setting`.
[settings]
site_name = "Corner Shop"
page_size = 20
[settings.api]
base = "https://api.example.com"
retries = 3

Read the page size

<?nvs
// Shows how to read the size of a page from the `[settings]` block of `nvs.toml`.
// `site_name` is a string in the file. This prints `Corner Shop`.
string $siteName = Core\Config::setting('site_name') as string;
echo $siteName, "\n";
// `page_size` is a whole number in the file, so `as int` works.
// This prints `Products 1 to 20`.
int $pageSize = Core\Config::setting('page_size') as int;
echo 'Products 1 to ', $pageSize, "\n";
// The second page starts after the first one. This prints `Page 2 starts at 21`.
echo 'Page 2 starts at ', $pageSize + 1, "\n";
Output
Corner Shop
Products 1 to 20
Page 2 starts at 21

Read a table of api settings

<?nvs
// Shows how to read the values of a table such as `[settings.api]`.
// A dotted name reads one key of the table.
// This prints `https://api.example.com/orders`.
string $base = Core\Config::setting('api.base') as string;
echo $base . '/orders', "\n";
// `retries` is an integer in the file, so the result is an `int`. This prints `3`.
int $retries = Core\Config::setting('api.retries') as int;
echo $retries, "\n";
// The name of the table returns all of its keys in one array.
// This prints `{"base":"https://api.example.com","retries":3}`.
echo Core\Json::encode(Core\Config::setting('api')), "\n";
Output
https://api.example.com/orders
3
{"base":"https://api.example.com","retries":3}

Use a default when a value is missing

<?nvs
// Shows how to use a default value when `nvs.toml` does not set a setting.
// `nvs.toml` does not set `cache_seconds`, so `as ?int` gives `null`.
// This prints `null`.
echo Core\Json::encode(Core\Config::setting('cache_seconds') as ?int), "\n";
// The `??` operator replaces `null` with a default value. This prints `60`.
int $seconds = Core\Config::setting('cache_seconds') as ?int ?? 60;
echo $seconds, "\n";
// `site_name` is text and not a whole number, so `as ?int` also gives `null`.
// This prints `60`.
echo Core\Config::setting('site_name') as ?int ?? 60, "\n";
Output
null
60
60