Description
Symfony version(s) affected
7.3.0
Description
Hello all,
I think I identified a routing problem with defaults / requirements, when the variable is on the host value.
Documentation here : https://symfony.com/doc/current/routing.html#sub-domain-routing, says that using a variable for the host to be matched, can work. And I agree on this.
However, it seems that, if no value is provided for the variable, the default value is not used.
I'm still on the lookout for the bit of code that could be responsible for this.
NB : I have this bug on 7.3.0, but it might present in older version as well.
How to reproduce
Reproducing in Symfony tests
You can had these tests in UrlMatcher.php
to see what's going on:
public function testSubdomainWithRequirementWithoutDefaults()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/', [], ['subdomain' => 'm|mobile'], [], '{subdomain}.example.com'));
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'GET', 'm.example.com'));
$this->assertEquals('foo', $matcher->match('/')['_route']);
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'GET', 'mobile.example.com'));
$this->assertEquals('foo', $matcher->match('/')['_route']);
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'GET', 'wrongsubdomain.example.com'));
try {
$matcher->match('/');
$this->fail();
} catch (ResourceNotFoundException $e) {
}
}
public function testSubdomainWithRequirementWithDefaults()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/', ['subdomain' => 'm'], ['subdomain' => 'm|mobile'], [], '{!subdomain}.example.com'));
$matcher = $this->getUrlMatcher($collection, new RequestContext('', 'GET', 'example.com'));
$this->assertEquals('foo', $matcher->match('/')['_route']);
}
testSubdomainWithRequirementWithDefaults
will fail.
Reproducing in an existing project
Add this route in a controller :
#[Route(path: '/symfony-subdomain-issue', name: 'symfony_subdomain_issue', requirements: ['subdomain' => '[a-zA-Z]+'], defaults: ['subdomain' => 'en'], host: '{!subdomain}.example.com')]
And use the following commands to check if route is matched or not :
bin/console router:match /symfony-subdomain-issue --host=en.example.com
bin/console router:match /symfony-subdomain-issue --host=example.com
Right now, the first command works, but the second does not match any route.
Possible Solution
No response
Additional Context
No response