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 an engine to compile and evaluate one-line expressions that return values (often booleans). Use it to embed simple, safe business rules and conditions in your app, with support for custom functions and variables.

View on GitHub
Deep Wiki
Context7

The Symfony ExpressionLanguage component provides a fast, extensible engine to parse, compile, and evaluate expressions in PHP. Use it to embed concise, readable rules (often returning booleans) for authorization, configuration, filtering, and other decision logic without hard-coding conditionals.

It supports custom variables and functions, and can compile expressions to PHP for performance in production.

  • Evaluate one-line expressions against a context of values
  • Compile to PHP for efficient execution and caching
  • Extend with custom functions and expression providers
  • Secure rule logic by centralizing and validating conditions
  • Integrate easily with other Symfony components and apps
Frequently asked questions about Expression Language
How do I integrate Symfony ExpressionLanguage into a Laravel 11+ project?
Add the package via Composer (`composer require symfony/expression-language`), then register it as a singleton in your `AppServiceProvider` using `$app->singleton(ExpressionLanguage::class, fn() => new ExpressionLanguage())`. Inject the service into controllers or policies via Laravel’s container.
Can I use this for Laravel’s policy-based authorization instead of `authorize()` methods?
Yes. Replace static `authorize()` logic with compiled expressions. For example, store rules like `'user.role === 'admin' && post.published'` in a database, then evaluate them dynamically in policies using `$expression->evaluate(['user' => $user, 'post' => $post])`.
What Laravel versions and PHP versions does Symfony ExpressionLanguage support?
The component requires PHP 8.1+ (for Symfony 8.x) or 8.4+ (latest). Laravel 11+ works seamlessly, but test with Laravel 10 if using Symfony 6.x. Check Symfony’s [documentation](https://symfony.com/doc/current/components/expression_language.html) for version-specific notes.
How do I cache compiled expressions to improve performance in high-traffic Laravel apps?
Use Laravel’s cache system to store compiled expressions. For example, cache the compiled policy rule for 1 hour: `$cache->remember('policy:edit_post', 3600, fn() => $compiler->compile($expression))`. This avoids recompiling the same logic repeatedly.
Is it safe to evaluate user-provided expressions (e.g., from a database) without risking code injection?
No, never evaluate raw user input directly. Whitelist allowed variables and functions (e.g., `user.role`, `post.isPublished`) and validate expressions against a schema. Use Symfony’s `ExpressionLanguage` with strict context restrictions to mitigate injection risks.
Can I use custom functions (e.g., `user.hasPermission()`) in expressions?
Yes. Register custom functions via `$expressionLanguage->addFunction()` in your service provider. For example, add `hasPermission` to check roles dynamically: `$expressionLanguage->addFunction(new FunctionNode('hasPermission', fn($args) => $user->hasPermission($args[0])));`.
How do I debug or troubleshoot malformed expressions in Laravel?
Log the compiled PHP code for inspection using `$expressionLanguage->getParser()->parse($expression)`. For runtime errors, wrap evaluations in try-catch blocks and log the expression context. Symfony’s error messages will detail syntax issues.
What’s the performance difference between compiled and interpreted expressions in Laravel?
Compiled expressions are significantly faster—benchmarks show 10x+ speedup for repeated evaluations (e.g., API rate limiting). Interpreted expressions add runtime parsing overhead. Always compile expressions used in loops or high-frequency checks like middleware.
Can I use this for Laravel validation rules stored in a database (e.g., dynamic validation logic)?
Absolutely. Extend Laravel’s `Validator` to evaluate expressions from a database. For example, fetch rules like `'length > 5 && contains @'` from a `validation_rules` table and pass them to `$expression->evaluate($data)`.
What are the alternatives to Symfony ExpressionLanguage for dynamic rules in Laravel?
Alternatives include Laravel’s native `when()`/`unless()` methods (for simple conditions), custom parser libraries like `league/expression-language`, or DIY solutions with `eval()` (discouraged due to security risks). Symfony’s component is preferred for its performance, extensibility, and security model.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport