8000 [Routing] added hostname support in UrlMatcher · symfony/symfony@11b4378 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11b4378

Browse files
fabpotarnaud-lb
authored andcommitted
[Routing] added hostname support in UrlMatcher
1 parent fc015d5 commit 11b4378

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
117117
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
118118
continue;
119119
}
120+
$hostnameMatches = array();
121+
122+
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
123+
continue;
124+
}
120125

121126
// check HTTP method requirement
122127
if ($req = $route->getRequirement('_method')) {
@@ -142,7 +147,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
142147
continue;
143148
}
144149

145-
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
150+
return array_merge($this->mergeDefaults($hostnameMatches + $matches, $route->getDefaults()), array('_route' => $name));
146151
}
147152
}
148153

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,4 @@ public function setHostnamePattern($pattern)
367367
}
368368
}
369369
}
370-
371370
}

src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,39 @@ public function testDecodeOnce()
328328
$matcher = new UrlMatcher($coll, new RequestContext());
329329
$this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
330330
}
331+
332+
public function testWithHostname()
333+
{
334+
$coll = new RouteCollection();
335+
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
336+
337+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
338+
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
339+
}
340+
341+
public function testWithHostnameOnRouteCollection()
342+
{
343+
$coll = new RouteCollection();
344+
$coll->add('foo', new Route('/foo/{foo}'));
345+
$coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
346+
$coll->setHostnamePattern('{locale}.example.com');
347+
348+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
349+
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
350+
351+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.net'));
352+
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
353+
}
354+
355+
/**
356+
* @expectedException Symfony\Component\Routing\Exception\ResourceNotFoundException
357+
*/
358+
public function testWithOutHostnameHostnameDoesNotMatch()
359+
{
360+
$coll = new RouteCollection();
361+
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
362+
363+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
364+
$matcher->match('/foo/bar');
365+
}
331366
}

0 commit comments

Comments
 (0)
0