- How does grasmash/expander help with feature rollouts in Laravel?
- Expander lets you define dynamic expansions (e.g., `${feature.enabled}`) tied to environments, user roles, or percentages. Use it in config files, API responses, or middleware to toggle features without code changes or redeploys. For example, enable a beta feature for 10% of users via `${user.is_beta:10%}`.
- Can I use Expander to replace hardcoded values in Laravel’s config files?
- Yes. Replace static values in `config/expander.php` with dynamic references like `${env.DB_CONNECTION}` or `${cache.driver}`. The package recursively resolves dot-notation (e.g., `${app.settings.timeout}`) at runtime, making config files adaptable across environments.
- Does Expander work with Laravel’s Eloquent models?
- Not natively, but you can manually register expanders for models. For example, add `addExpander('user', fn($id) => User::find($id))` to resolve `${user.posts.0.title}`. This requires upfront setup but avoids ORM-specific optimizations like eager loading.
- What happens if a reference like `${missing.key}` can’t be resolved?
- By default, unresolved references remain as `${missing.key}`. To enforce strict validation, extend the `Expander` class or wrap calls in a try-catch block. For Laravel APIs, integrate with `ProblemDetails` middleware to return 400 errors for broken references.
- Will Expander slow down my Laravel API with deeply nested arrays?
- Recursive expansion can introduce latency for payloads with 20+ nested levels. Test performance in your staging environment. Mitigate by caching expanded responses (e.g., via Laravel’s `cache()->remember`) or limiting expansion to critical paths like API responses.
- How do I integrate Expander with Laravel’s request validation?
- Use middleware to expand request data before validation. For example, in `ExpandPayloadMiddleware`, call `$expander->expand($request->all())` before passing to Laravel’s validator. This ensures dynamic values (e.g., `${user.id}`) are resolved before rules like `required` are applied.
- Can I use Expander for A/B testing in Laravel?
- Yes. Define experiment rules like `${experiment.variant:50%}` in your config or API responses. Combine with Laravel’s `session()` or `cache()` to track user participation. For example, expand `${feature.new_ui:${user.is_test:50%}}` to toggle UI variants.
- Does Expander support Laravel 10+ and PHP 8.1+?
- Yes, Expander is compatible with Laravel 9/10 and PHP 8.0+. The package uses modern PHP features like named arguments and strict typing. Check the [GitHub actions badge](https://github.com/grasmash/expander/actions) for the latest test matrix.
- How do I test Expander in Laravel’s Pest/PHPUnit?
- Mock the `Expander` instance to return predefined resolutions. For example, in Pest: `$expander = Mockery::mock(Expander::class)->shouldReceive('expand')->andReturn(['resolved' => 'data'])->once()`. Test edge cases like empty arrays, null values, and circular references.
- Are there alternatives to Expander for Laravel feature flags?
- For feature flags, consider Spatie’s `laravel-feature-flags` (database-backed) or `spatie/array-to-xml` (for XML expansion). For dynamic config, Laravel’s `config()` helper with environment variables works for simple cases. Expander stands out for nested array/JSON expansion without ORM dependencies.