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

Expander Laravel Package

grasmash/expander

Expander is a Laravel package for managing feature rollouts and gating functionality. It helps you define “expansions” that can be enabled per environment, user, or percentage, making it easy to ship safely, run experiments, and toggle features without redeploys.

View on GitHub
Deep Wiki
Context7

This tool expands property references in PHP arrays.

Frequently asked questions about Expander
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.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata