8000 [Routing] Add a '_host' requirement for routes by sebhoerl · Pull Request #3002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Add a '_host' requirement for routes #3002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Routing] Added host requirement
  • Loading branch information
sebhoerl committed Dec 30, 2011
commit 59ad99d345eb75ddd0d7e5f70e7f5dccc2a87607
7 changes: 6 additions & 1 deletion src/Symfony/Component/Routing/Generator/UrlGenerator.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa

if ($this->context->getHost()) {
$scheme = $this->context->getScheme();
$host = $this->context->getHost();
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
$absolute = true;
$scheme = $req;
}
if (isset($requirements['_host']) && ($req = $requirements['_host']) && $host != $req) {
$absolute = true;
$host = $req;
}

if ($absolute) {
$port = '';
Expand All @@ -159,7 +164,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$port = ':'.$this->context->getHttpsPort();
}

$url = $scheme.'://'.$this->context->getHost().$port.$url;
$url = $scheme.'://'.$host.$port.$url;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function dump(array $options = array())

$rule = array("# $name");

if($hostreq = $route->getRequirement('_host')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after if

$rule[] = "RewriteCond %{HTTP_HOST} $hostreq";
}

// method mismatch
if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtoupper($req));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
if ($conditions) {
EOF;

if ($hostreq = $route->getRequirement('_host')) {
$code[] = <<<EOF
if(\$this->context->getHost() !== '$hostreq') {
goto $gotoname;
}
EOF;
}

if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtoupper($req));
// GET and HEAD are equivalent
Expand Down Expand Up @@ -239,7 +247,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}
$code[] = " }";

if ($req) {
if ($req || $hostreq) {
$code[] = " $gotoname:";
}

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
continue;
}

if ($hostreq = $route->getRequirement('_host')) {
if ($hostreq !== $this->context->getHost()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply $route->getRequirement('_host') !== $this->context->getHost() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's neccessary to test whether the requirement is set at all. Your condition would reject every route that has no _host requirement defined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Wrotten to fast ;-) But it can be merged into one if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that'd be ok :)

continue;
}
}

// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
Expand Down
17 changes: 17 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ public function testNoMethodSoAllowed()
$matcher->match('/foo');
}

public function testHostRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_host' => 'example.org')));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'example.org'));
$this->assertEquals(array('_route' => 'foo'), $matcher->match( '/foo' ) );


$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'other.host'));
try {
$matcher->match( '/foo' );
$this->fail();
} catch (ResourceNotFoundException $e) {}

}

public function testMethodNotAllowed()
{
$coll = new RouteCollection();
Expand Down
0