Installation
composer require yosymfony/toml
No additional configuration is required—this is a drop-in parser with zero dependencies.
First Use Case: Parsing a TOML File
use Yosymfony\Toml\Toml;
$toml = file_get_contents('config.toml');
$data = Toml::parse($toml);
// Outputs: `['database' => ['host' => 'localhost', 'port' => 3306]]`
print_r($data);
Where to Look First
Toml::parse() (string input) and Toml::parseFile() (file path).inline_tables, dates).Toml::validate() to check for syntax errors before parsing.Configuration Management
// Load Laravel config from TOML (e.g., `config/services.toml`)
$services = Toml::parseFile(config_path('services.toml'));
config(['services' => $services]);
Dynamic Configuration Overrides
// Merge TOML overrides (e.g., `.env.toml` or user-provided configs)
$overrides = Toml::parseFile(storage_path('config/overrides.toml'));
$finalConfig = array_merge_recursive(
config('default'),
$overrides
);
CLI Argument Parsing
// Parse TOML-formatted CLI args (e.g., `--config=deploy.toml`)
$config = Toml::parse(file_get_contents($argv[1]));
Integration with Laravel Service Providers
public function boot()
{
$this->app->singleton('toml-config', function () {
return Toml::parseFile(config_path('custom.toml'));
});
}
$cacheKey = 'toml:config';
$config = cache()->remember($cacheKey, now()->addHours(1), function () {
return Toml::parseFile(config_path('app.toml'));
});
$data['port'] = (int) $data['port'];
$data['enabled'] = (bool) $data['enabled'];
Validator to enforce TOML schema rules:
$validator = Validator::make($data, [
'database.host' => 'required|string',
'database.port' => 'required|integer|between:1,65535',
]);
String vs. Number/Boolean Parsing
// ❌ Fails silently (returns "123" instead of 123)
$port = $toml['port'];
// ✅ Correct
$port = (int) $toml['port'];
Date/Time Handling
1979-05-27T07:32:00Z) are parsed as strings. Use Carbon:
use Carbon\Carbon;
$date = Carbon::parse($toml['date']);
Inline Tables vs. Arrays
# Inline table (parsed as associative array)
key = { nested = "value" }
# Array (parsed as indexed array)
key = [1, 2, 3]
File Encoding
Deprecated Methods
Toml::load() (deprecated in favor of parseFile()).Validate Before Parsing
if (!Toml::validate($tomlContent)) {
throw new \RuntimeException('Invalid TOML: ' . Toml::getLastError());
}
Inspect Raw Output
Use print_r() or var_export() to debug parsed structures:
var_export(Toml::parse($toml));
Test Edge Cases
{}).true, false, null, +/inf, -/nan).Custom Parsers Extend the parser for domain-specific TOML (e.g., add Laravel-specific defaults):
class LaravelToml extends Toml
{
public static function parseLaravel($toml)
{
$data = parent::parse($toml);
$data['app']['env'] = env('APP_ENV', 'production');
return $data;
}
}
TOML Dump Support
Use spatie/fork or league/config for dumping PHP arrays back to TOML (this package is read-only).
Integration with Laravel Filesystem
Combine with Illuminate/Filesystem for dynamic TOML loading:
$toml = Storage::disk('config')->get('settings.toml');
$config = Toml::parse($toml);
Schema Validation
Pair with webonyx/graphql-php or spatie/laravel-validation-extensions for TOML schema validation.
.toml or .env-style config. All settings are parsed at runtime.Database ≠ database).How can I help you explore Laravel packages today?