Weave 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

Config Laravel Package

respect/config

Respect\Config is a lightweight PHP configuration package with a clean API for loading, merging, and accessing settings from arrays and files. It supports dot-notation, validation, and framework integration, making application configuration simple and predictable.

Deep Wiki
Context7

Getting Started

First Steps

  1. Installation Add the package via Composer:

    composer require respect/config
    

    Register the service provider in config/app.php under providers:

    Respect\Config::class,
    
  2. Basic Configuration Define a config file (e.g., config/app.php):

    return [
        'debug' => env('APP_DEBUG', false),
        'database' => [
            'default' => 'mysql',
            'connections' => [
                'mysql' => [
                    'driver' => 'mysql',
                    'host' => env('DB_HOST', 'localhost'),
                ],
            ],
        ],
    ];
    
  3. First Usage Access config values via the Config facade:

    use Respect\Config;
    
    $debug = Config::get('debug'); // false
    $host = Config::get('database.connections.mysql.host'); // 'localhost'
    

First Use Case: Environment-Based Config Overrides

Use Config::set() to dynamically override values (e.g., in a service provider):

Config::set('debug', true); // Override for local/dev

Implementation Patterns

1. Dependency Injection (DI) with Config

Leverage Config for lightweight DI in constructors:

class UserService {
    public function __construct(
        private string $dbHost,
        private bool $debugMode
    ) {
        $this->dbHost = Config::get('database.connections.mysql.host');
        $this->debugMode = Config::get('debug');
    }
}

2. Nested Config with Dot Notation

Access deeply nested configs fluently:

$timeout = Config::get('http.client.timeouts.connect'); // 5

3. Dynamic Config Merging

Merge configs at runtime (e.g., for feature flags):

Config::merge([
    'features' => [
        'new_ui' => env('FEATURE_NEW_UI', false),
    ],
]);

4. Config Validation

Validate configs during bootstrapping (e.g., in AppServiceProvider):

$config = Config::all();
if (!isset($config['database']['connections'])) {
    throw new \RuntimeException('Database config missing!');
}

5. Environment-Specific Configs

Load environment-specific configs (e.g., config/local.php):

if (app()->environment('local')) {
    Config::merge(require __DIR__.'/local.php');
}

Gotchas and Tips

Pitfalls

  1. No Caching by Default Config values are not cached between requests. For performance, cache Config::all() in a singleton or Laravel’s cache:

    $cachedConfig = Cache::remember('config', 60, fn() => Config::all());
    
  2. Overwriting vs. Merging Config::set() overwrites existing keys. Use Config::merge() for partial updates:

    Config::set('debug', true); // Overwrites entirely
    Config::merge(['debug' => true]); // Preserves other keys
    
  3. Dot Notation Ambiguity Avoid keys with dots (e.g., api.v1) if using Laravel’s dot notation helpers (e.g., config('api.v1')). Stick to underscores or validate paths:

    Config::get('api_v1.url'); // Safer
    
  4. Thread Safety Config is not thread-safe. Avoid modifying configs in queues or parallel processes without synchronization.


Debugging Tips

  1. Dump Entire Config Use Config::all() to inspect the full config tree during development:

    dd(Config::all());
    
  2. Validate Paths Check for typos in config paths with:

    if (!Config::has('database.connections.mysql')) {
        throw new \InvalidArgumentException('Invalid config path');
    }
    
  3. Environment Debugging Log environment-specific configs:

    \Log::debug('Current config:', ['env' => app()->environment(), 'config' => Config::all()]);
    

Extension Points

  1. Custom Config Loaders Extend Respect\Config by adding a loader for remote configs (e.g., Redis, API):

    Config::extend('remote', function () {
        return json_decode(file_get_contents('https://api.example.com/config'), true);
    });
    Config::merge(Config::get('remote'));
    
  2. Config Events Trigger events on config changes (e.g., for logging or analytics):

    Config::onChange(function ($key, $oldValue, $newValue) {
        \Log::info("Config changed: {$key} from {$oldValue} to {$newValue}");
    });
    
  3. Integration with Laravel’s Config Bridge respect/config with Laravel’s config() helper by aliasing:

    if (!function_exists('config')) {
        function config($key = null, $default = null) {
            return Respect\Config::get($key, $default);
        }
    }
    
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.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log