Repository navigation
Description
API Platform version(s) affected: 5.0.6 (Router::match() is the same in 5.0.7 and on main)
Description
ApiPlatform\Symfony\Routing\Router::match(), through which an IRI is turned back into its resource, matches against a RequestContext it builds anew from a request of its own, and copies only the path, the scheme and the host from the router's context. The context's parameters are lost, and with them _functions, through which FrameworkBundle gives route conditions their functions, such as env() (the setParameters() call on router.request_context).
So once a route's condition calls env(), for instance to match a host taken from an environment variable (condition: "request.getHost() == env('API_HOST')"), every IRI on that route throws Error: Call to a member function get() on null from the compiled matcher, where Symfony's router matches the same path.
How to reproduce
<?php
require __DIR__.'/vendor/autoload.php';
use ApiPlatform\Symfony\Routing\Router as ApiPlatformRouter;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Routing\Loader\ClosureLoader;
use Symfony\Component\Routing\Matcher\ExpressionLanguageProvider;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router;
// What FrameworkBundle wires: env() in a route's condition reaches the matcher through the context's "_functions" parameter
$functions = new ExpressionLanguageProvider(new ServiceLocator(['env' => static fn () => static fn (string $name) => $_SERVER[$name] ?? null]));
$context = new RequestContext(host: 'api.example.com');
$context->setParameter('_functions', $functions);
$router = new Router(new ClosureLoader(), static function (): RouteCollection {
$routes = new RouteCollection();
$routes->add('book', new Route('/books/{id}', condition: "request.getHost() == env('API_HOST')"));
return $routes;
}, ['cache_dir' => sys_get_temp_dir().'/'.uniqid('routes-')], $context);
$router->addExpressionLanguageProvider($functions);
$_SERVER['API_HOST'] = 'api.example.com';
var_dump($router->match('/books/1')['_route']);
var_dump((new ApiPlatformRouter($router))->match('/books/1')['_route']);It prints:
string(4) "book"
PHP Fatal error: Uncaught Error: Call to a member function get() on null in /tmp/routes-6af87c60caad1/url_matching_routes.php:25
Possible Solution
Keep the base context's parameters in match():
$context->setHost($baseContext->getHost());
$context->setParameters($baseContext->getParameters());With that line, the script's second match answers book too.
Additional Context
Until then, we decorate api_platform.router to match against a copy of the router's context.
Activity
fix(symfony): keep the router context parameters when matching an IRI (…
soyuka commentedon Nov 18, 2026
Fixed by #8859, thanks!