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
Expression Language

Expression Language Laravel Package

symfony/expression-language

Symfony ExpressionLanguage provides a fast engine to evaluate or compile short expressions into PHP. Use it to compute values or boolean rules, with custom functions and variables, for dynamic logic in apps and components.

View on GitHub
Deep Wiki
Context7

Provides an engine that can compile and evaluate expressions

Frequently asked questions about Expression Language
How do I integrate Symfony ExpressionLanguage into a Laravel project?
Install via Composer with `composer require symfony/expression-language`, then bind it in a service provider using Laravel’s container. For example, register it as a singleton with `app()->bind(ExpressionLanguage::class, fn() => new ExpressionLanguage())`. You can then resolve it anywhere in your app.
Can I use ExpressionLanguage for Laravel’s authorization (Gates/Policies) instead of hard-coded logic?
Yes. Replace verbose `if` chains in Gates or Policies with database-backed expressions. For example, store rules like `'user.role == 'admin' && request.method == 'POST'` in a `permissions` table, then evaluate them dynamically. This decouples logic from code and makes it configurable.
What Laravel versions and PHP versions does Symfony ExpressionLanguage support?
ExpressionLanguage works with Laravel 10+ (PHP 8.1+) and Symfony 7.x by default. For PHP 8.0, use Symfony 6.4. Always check the [Symfony docs](https://symfony.com/doc/current/components/expression_language/supported_versions.html) for exact compatibility, as newer Laravel versions may require Symfony 8.x for PHP 8.4+.
How do I cache compiled expressions for better performance in Laravel?
Cache compiled expressions using Laravel’s cache system. For example, wrap the compiler in a closure: `Cache::remember('expr_'.$hash, 3600, fn() => $compiler->compile($expression))`. This avoids recompiling the same expressions repeatedly, which is critical for high-frequency evaluations like API rate limiting.
Is ExpressionLanguage secure for evaluating user-provided input (e.g., feature flags from a database)?
No, it’s not safe by default. Always whitelist allowed variables using `ExpressionLanguage::setVariables(['user', 'request'])` and validate inputs against a schema. Avoid passing raw user input directly—sanitize or restrict context variables to trusted sources only.
Can I extend ExpressionLanguage with custom functions for domain-specific logic (e.g., `user.hasPermission('edit_post')`)?
Absolutely. Use `ExpressionLanguage::addFunction()` to register custom functions. For example, add a `hasPermission` function that checks against your Laravel permissions system. This lets you create domain-specific expressions like `'user.hasPermission('edit_post') && post.published'`.
How does ExpressionLanguage compare to Laravel’s Gates/Policies for authorization?
ExpressionLanguage is more flexible for dynamic rules stored in a database, while Gates/Policies are better for static, code-defined logic. Use ExpressionLanguage when rules change frequently (e.g., A/B testing, role-based conditions) and Gates/Policies for hard-coded authorization. You can even combine them by evaluating expressions in a Policy’s `authorize()` method.
Will ExpressionLanguage work with Laravel Nova or Filament for non-technical users to manage rules?
Yes, but you’ll need to build a custom UI. Use Nova/Filament’s form fields to let admins input expressions (e.g., a text field for `'user.isAdmin() && date.isWeekend()'`). Validate inputs server-side to ensure they only use allowed functions/variables. Consider a JSON editor or autocomplete for better UX.
How do I debug or log evaluated expressions in Laravel?
Log the compiled expression using `$compiler->getCompiledExpression()` before evaluation. For runtime debugging, wrap evaluations in a try-catch block and log errors with context (e.g., `Log::error('Failed to evaluate: '.$expression, ['context' => $variables])`). Use Xdebug to step through the compiled PHP code if needed.
Are there alternatives to ExpressionLanguage for dynamic logic in Laravel?
For simple cases, Laravel’s Gates/Policies or middleware suffice. For more advanced needs, consider packages like `spatie/laravel-permission` (for RBAC) or `laravel-rule-object` (for reusable rule objects). However, ExpressionLanguage stands out for its compiled performance, extensibility, and ability to handle arbitrary one-liner conditions without writing classes.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai