Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expression language support #14

Merged
merged 7 commits into from
Jul 1, 2024
Merged

Add expression language support #14

merged 7 commits into from
Jul 1, 2024

Conversation

mpdude
Copy link
Member

@mpdude mpdude commented Jun 26, 2024

Currently, the params used in ObjectRoute definitions are property path expressions (https://symfony.com/doc/current/components/property_access.html).

For certain use cases, more flexibility would be helpful. For example, assume a blog post can be archived. When you link to such a blog post, you need to include the post's year in an extra URL parameter: ?year=....

Something like #[ObjectRoute(..., params: ['year' => 'year'])] does not work, since the property access component cannot evaluate conditional expressions.

This PR adds a new configuration parameter named paramExpressions and uses the Symfony ExpressionLanguage component to evaluate it.

Example: #[ObjectRoute(..., paramExpressions: ['year' => 'this.isArchived ? this.year : null'])]

Expressions given in paramExpressions can access two variables: this is the object on which the route is being generated, and params gives access to all parameter values. Those are the combination of the extraParams passed to the object router, and all params evaluated through property access expressions as previously.

In order to make optional parameters possible, the parameter name in paramExpressions can be prefixed with ? to indicate that a parameter should not be used (filtered out) when the expression evaluates to null. So, #[ObjectRoute(..., paramExpressions: ['?year' => 'this.isArchived ? this.year : null'])] would only pass the year parameter to the underlying router when the blog post has been archived.

Resolves #12.

@mpdude mpdude marked this pull request as ready for review July 1, 2024 08:05
@mpdude mpdude merged commit 2e91b32 into master Jul 1, 2024
4 checks passed
@mpdude mpdude deleted the expression-language-poc branch July 1, 2024 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support expression language for route params
1 participant