[Routing] added hostname support in UrlMatcher
This commit is contained in:
parent
fc015d5227
commit
11b4378238
@ -117,6 +117,11 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
|
||||
continue;
|
||||
}
|
||||
$hostnameMatches = array();
|
||||
|
||||
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// check HTTP method requirement
|
||||
if ($req = $route->getRequirement('_method')) {
|
||||
@ -142,7 +147,7 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
|
||||
return array_merge($this->mergeDefaults($hostnameMatches + $matches, $route->getDefaults()), array('_route' => $name));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -367,5 +367,4 @@ class RouteCollection implements \IteratorAggregate, \Countable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -328,4 +328,39 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
$matcher = new UrlMatcher($coll, new RequestContext());
|
||||
$this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
|
||||
}
|
||||
|
||||
public function testWithHostname()
|
||||
{
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
|
||||
|
||||
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
|
||||
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
|
||||
}
|
||||
|
||||
public function testWithHostnameOnRouteCollection()
|
||||
{
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('foo', new Route('/foo/{foo}'));
|
||||
$coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
|
||||
$coll->setHostnamePattern('{locale}.example.com');
|
||||
|
||||
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
|
||||
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
|
||||
|
||||
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.net'));
|
||||
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
*/
|
||||
public function testWithOutHostnameHostnameDoesNotMatch()
|
||||
{
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
|
||||
|
||||
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
|
||||
$matcher->match('/foo/bar');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user