The short version: What objects do I, the end user programmer, have access to when using Symfony's routing conditions?
The long version: Symfony routes allow you to use a key named condition
.
contact:
path: /contact
controller: 'App\Controller\DefaultController::contact'
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"
The value of condition
is code -- it's a Symfony Domain Specific Language (DSL) based on (but not identical to?) the twig templating language syntax. The Symfony docs refer to this as "The Expression Syntax".
That much I've been able to glean from the documentation. What I can't figure out is what object I'll have access to using this DSL? i.e. in the example above the expression seems to have access to a context
object and a request
object.
Are there others? Are there docs or a place in the source code where I can see what other objects I'd have access to with condition
?
The documentation you are linking states that only these two objects are available in the expression:
You can do any complex logic you need in the expression by leveraging two variables that are passed into the expression:
context - An instance of
RequestContext
, which holds the most fundamental information about the route being matched.request - The Symfony
Request
object (see Request).
(Emphasis mine).
You can see where these objects are injected into the expression on Symfony\Component\Routing\Matcher\UrlMatcher::handleRouteRequirements()
:
protected function handleRouteRequirements($pathinfo, $name, Route $route)
{
// expression condition
if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), ['context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)])) {
return [self::REQUIREMENT_MISMATCH, null];
}
return [self::REQUIREMENT_MATCH, null];
}
The call to evaluate()
passes both the expression you defined on the condition
key, and an array with $context
and $request
.