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

Skip to content

Commit 1203bf5

Browse files
committed
[Routing] added hostname support in UrlMatcher
1 parent 704613e commit 1203bf5

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
@@ -132,6 +132,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
132132
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
133133
continue;
134134
}
135+
$hostnameMatches = array();
136+
137+
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
138+
continue;
139+
}
135140

136141
// check HTTP method requirement
137142
if ($req = $route->getRequirement('_method')) {
@@ -157,7 +162,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
157162
continue;
158163
}
159164

160-
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
165+
return array_merge($this->mergeDefaults($hostnameMatches + $matches, $route->getDefaults()), array('_route' => $name));
161166
}
162167
}
163168

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,4 @@ public function setHostnamePattern($pattern)
352352
}
353353
}
354354
}
355-
356355
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,39 @@ public function testDecodeOnce()
225225
$matcher = new UrlMatcher($coll, new RequestContext());
226226
$this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
227227
}
228+
229+
public function testWithHostname()
230+
{
231+
$coll = new RouteCollection();
232+
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
233+
234+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
235+
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
236+
}
237+
238+
public function testWithHostnameOnRouteCollection()
239+
{
240+
$coll = new RouteCollection();
241+
$coll->add('foo', new Route('/foo/{foo}'));
242+
$coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
243+
$coll->setHostnamePattern('{locale}.example.com');
244+
245+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
246+
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
247+
248+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.net'));
249+
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
250+
}
251+
252+
/**
253+
* @expectedException Symfony\Component\Routing\Exception\ResourceNotFoundException
254+
*/
255+
public function testWithOutHostnameHostnameDoesNotMatch()
256+
{
257+
$coll = new RouteCollection();
258+
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
259+
260+
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
261+
$matcher->match('/foo/bar');
262+
}
228263
}

0 commit comments

Comments
 (0)
0