Installation
composer require bitscout/simple-config
Add to bundles.php:
Bitscout\SimpleConfig\BitscoutSimpleConfigBundle::class => ['all' => true],
Configuration
Create config/packages/bitscout_simple_config.yaml:
bitscout_simple_config:
fields:
site_name:
label: "Site Name"
type: string
default: "My App"
show: true
This injects SIMPLE_CONFIG_SITE_NAME as an environment variable.
Accessing Values Retrieve values via:
$siteName = $_ENV['SIMPLE_CONFIG_SITE_NAME'] ?? config('bitscout_simple_config.fields.site_name.default');
Or use Laravel’s config helper:
$siteName = config('bitscout_simple_config.fields.site_name.default');
Admin Integration
Add a SonataAdmin menu entry in sonata_admin.yaml:
menu:
main:
- { route: admin_app_bitscout_simple_config_list, label: 'Config', icon: '<i class="fa fa-cog"></i>' }
Define Config Fields
Use bitscout_simple_config.yaml to declare all configurable fields:
bitscout_simple_config:
fields:
maintenance_mode:
label: "Maintenance Mode"
type: boolean
default: false
api_timeout:
label: "API Timeout (seconds)"
type: int
default: 30
Override Defaults via Admin
admin_app_bitscout_simple_config_list).Runtime Access
$_ENV['SIMPLE_CONFIG_KEY'] for runtime overrides.config('bitscout_simple_config.fields.key.default') if env vars are missing.$this->app->bind('config.resolver', function ($app) {
return new ConfigResolver($app['config']);
});
Conditional Logic Use config values to drive feature flags or environment-specific behavior:
if ($_ENV['SIMPLE_CONFIG_MAINTENANCE_MODE'] === 'true') {
abort(503, 'Site under maintenance');
}
Validation Extend the bundle’s validation logic (if supported) or validate manually:
$timeout = (int) ($_ENV['SIMPLE_CONFIG_API_TIMEOUT'] ?? 30);
if ($timeout < 10 || $timeout > 60) {
throw new \InvalidArgumentException('Timeout must be between 10 and 60 seconds.');
}
SonataAdmin Menu Grouping Organize config entries under a logical group (e.g., "Settings") by leveraging Sonata’s menu hierarchy.
Environment-Specific Overrides
Use Laravel’s env() function to merge config with .env:
$value = env('SIMPLE_CONFIG_CUSTOM_VAR', config('bitscout_simple_config.fields.custom_var.default'));
Caching Cache resolved config values if performance is critical:
$cacheKey = 'config.site_name';
$siteName = Cache::remember($cacheKey, now()->addHours(1), function () {
return $_ENV['SIMPLE_CONFIG_SITE_NAME'] ?? config('bitscout_simple_config.fields.site_name.default');
});
Testing Mock config values in tests:
$_ENV['SIMPLE_CONFIG_TEST_MODE'] = 'true';
$this->assertTrue($_ENV['SIMPLE_CONFIG_TEST_MODE']);
Bundle Incompleteness
Config model/table and override the bundle’s behavior.Environment Variable Pollution
SIMPLE_CONFIG_*), which may conflict with existing .env variables.APP_SIMPLE_CONFIG_*).Type Safety
type field (e.g., int, boolean) is not enforced by the bundle. User inputs may corrupt data.Missing Documentation
vendor/bitscout/simple-config) for clues.SonataAdmin Dependency
admin_app_bitscout_simple_config_list) will 404.Check Config Loading Verify the YAML file is parsed correctly:
php artisan config:clear
php artisan config:cache
Then dump the config:
dd(config('bitscout_simple_config'));
Environment Variable Conflicts
If SIMPLE_CONFIG_* vars are ignored, check:
.env file for overrides.Admin Route Issues Ensure:
sonata_admin.yaml before the bundle tries to use it.Custom Storage
Override the default storage (likely a simple array or file-based) by extending the bundle’s ConfigManager:
// config/packages/bitscout_simple_config.php
return [
'storage' => [
'driver' => 'database',
'table' => 'simple_config',
],
];
Add Validation Extend the SonataAdmin form type to add validation rules:
// src/Form/Type/ConfigType.php
use Symfony\Component\Validator\Constraints as Assert;
$builder->add('value', TextType::class, [
'constraints' => [
new Assert\Type(['type' => 'numeric']),
new Assert\Range(['min' => 1, 'max' => 100]),
],
]);
Event Listeners Listen for config changes to trigger actions (e.g., cache clearing):
// EventSubscriber
public function onConfigUpdate(ConfigUpdateEvent $event)
{
Cache::clear();
}
Localization Support multi-language labels by extending the config structure:
bitscout_simple_config:
fields:
site_name:
label:
en: "Site Name"
fr: "Nom du Site"
Then resolve dynamically:
$label = config("bitscout_simple_config.fields.site_name.label.{$app->getLocale()}");
Use for Non-Critical Settings Since the bundle is unfinished, limit its use to low-risk configurations (e.g., feature toggles, non-sensitive defaults).
Fallback to Database
If the bundle lacks database storage, implement a simple config_values table:
// Migration
Schema::create('config_values', function (Blueprint $table) {
$table->string('key')->unique();
$table->text('value')->nullable();
$table->timestamps();
});
Combine with Laravel Config Merge SimpleConfig with Laravel’s native config for a unified approach:
# config/app.php
'site' => [
'name' => env('SIMPLE_CONFIG_SITE_NAME', config('bitscout_simple_config.fields.site_name.default')),
],
How can I help you explore Laravel packages today?