Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Settings Bundle Laravel Package

chamilo/settings-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle via Composer:

    composer require chamilo/settings-bundle
    

    Register it in config/bundles.php:

    return [
        // ...
        Chamilo\SettingsBundle\ChamiloSettingsBundle::class => ['all' => true],
    ];
    
  2. Configuration Define settings in config/packages/chamilo_settings.yaml:

    chamilo_settings:
        channels:
            default:
                settings:
                    app_name: { type: text, value: 'My App' }
                    max_upload_size: { type: integer, value: 10 }
    
  3. First Use Case Access a setting in a controller:

    use Chamilo\SettingsBundle\Settings\SettingsInterface;
    
    public function index(SettingsInterface $settings)
    {
        $appName = $settings->get('default', 'app_name');
        return new Response($appName);
    }
    
  4. Admin Interface Enable the admin panel by adding the route:

    # config/routes.yaml
    chamilo_settings_admin:
        resource: "@ChamiloSettingsBundle/Resources/config/routing/admin.yaml"
        prefix: /admin/settings
    

    Access /admin/settings to edit settings via UI.


Implementation Patterns

Workflows

  1. Structured Settings Organize settings by channels (e.g., default, api, frontend):

    chamilo_settings:
        channels:
            api:
                settings:
                    rate_limit: { type: integer, value: 100 }
    

    Access via:

    $settings->get('api', 'rate_limit');
    
  2. Dynamic Types Support for text, integer, boolean, select, textarea, and custom types. Extend with:

    custom_types:
        color: Chamilo\SettingsBundle\Type\ColorType
    
  3. Validation Add validation rules in YAML:

    settings:
        max_users: { type: integer, value: 50, validation: { min: 10, max: 100 } }
    
  4. Environment Overrides Override settings per environment (e.g., config/packages/dev/chamilo_settings.yaml):

    chamilo_settings:
        channels:
            default:
                settings:
                    debug_mode: { type: boolean, value: true }
    
  5. Event Listeners React to setting changes via events:

    // src/EventListener/SettingsListener.php
    public function onSettingsUpdate(SettingsUpdateEvent $event)
    {
        $channel = $event->getChannel();
        $name = $event->getName();
        // Logic here
    }
    

    Register in services.yaml:

    services:
        App\EventListener\SettingsListener:
            tags:
                - { name: kernel.event_listener, event: chamilo.settings.update, method: onSettingsUpdate }
    
  6. Caching Enable caching for performance:

    chamilo_settings:
        cache: true
        cache_pool: cache.app
    

Gotchas and Tips

Pitfalls

  1. Channel Mismatches

    • Issue: Forgetting to specify a channel when accessing settings.
      // ❌ Throws exception
      $settings->get('nonexistent_channel', 'key');
      
    • Fix: Always validate channels exist in YAML or dynamically.
  2. Type Casting

    • Issue: Incorrect type handling (e.g., select values not validated).
      settings:
          theme: { type: select, value: 'dark', choices: ['light', 'dark'] }
      
    • Fix: Ensure choices are exhaustive and use validation for strict checks.
  3. Admin Route Conflicts

    • Issue: Overlapping routes with other bundles (e.g., Symfony’s config).
    • Fix: Customize the admin prefix or use _controller in routing.
  4. Serialization Errors

    • Issue: Complex objects (e.g., arrays with non-scalar values) failing to serialize.
    • Fix: Use json type for complex data or flatten structures.
  5. Cache Invalidation

    • Issue: Settings not updating after changes due to stale cache.
    • Fix: Clear cache manually or use cache:clear:
      php bin/console cache:clear
      

Debugging Tips

  1. Dump All Settings Use a twig function or controller to debug:

    $settings->all(); // Returns array of all channels/settings
    

    Or in Twig:

    {{ dump(app('chamilo_settings.settings').all()) }}
    
  2. Check Event Dispatching Enable debug mode to verify events fire:

    framework:
        ide: phpstorm
    

    Add a dd() in your listener to confirm triggers.

  3. Validate YAML Use Symfony’s validator to catch YAML errors:

    php bin/console debug:config chamilo_settings
    

Extension Points

  1. Custom Types Create a new type class (e.g., JsonType):

    namespace App\Settings\Type;
    
    use Chamilo\SettingsBundle\Type\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    
    class JsonType extends AbstractType
    {
        public function getFormType()
        {
            return TextareaType::class;
        }
    
        public function getValue($value)
        {
            return json_decode($value, true);
        }
    }
    

    Register in config/packages/chamilo_settings.yaml:

    custom_types:
        json: App\Settings\Type\JsonType
    
  2. Override Templates Customize the admin UI by overriding templates:

    templates/
        ChamiloSettingsBundle/
            admin/
                settings/
                    index.html.twig
    
  3. Database Backend Extend to use a database (e.g., Doctrine) instead of YAML:

    use Chamilo\SettingsBundle\Settings\SettingsInterface;
    use Doctrine\DBAL\Connection;
    
    class DatabaseSettings implements SettingsInterface
    {
        public function __construct(private Connection $connection) {}
    
        public function get(string $channel, string $name, $default = null)
        {
            $stmt = $this->connection->prepare('SELECT value FROM settings WHERE channel = ? AND name = ?');
            $stmt->execute([$channel, $name]);
            return $stmt->fetchColumn() ?? $default;
        }
        // Implement other methods...
    }
    

    Bind in services.yaml:

    chamilo_settings.settings:
        class: App\Settings\DatabaseSettings
        arguments:
            - '@database_connection'
    
  4. API Access Expose settings via API using Symfony’s Serializer:

    use Symfony\Component\Serializer\SerializerInterface;
    
    public function getSettingsApi(SettingsInterface $settings, SerializerInterface $serializer)
    {
        $data = $settings->all();
        return new JsonResponse($serializer->serialize($data, 'json'));
    }
    
  5. Environment-Specific Defaults Use %kernel.environment% in YAML for dynamic defaults:

    settings:
        environment: { type: text, value: '%kernel.environment%' }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calliostro/spotify-bundle
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle