Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the
params
used inObjectRoute
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, andparams
gives access to all parameter values. Those are the combination of theextraParams
passed to the object router, and allparams
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 tonull
. So,#[ObjectRoute(..., paramExpressions: ['?year' => 'this.isArchived ? this.year : null'])]
would only pass theyear
parameter to the underlying router when the blog post has been archived.Resolves #12.