Skip to content

Commit

Permalink
Route: add %host% variable (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkrmela authored and dg committed Jun 7, 2016
1 parent a3e627d commit c0bcf7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ public function match(Nette\Http\IRequest $httpRequest)
if ($this->type === self::HOST) {
$host = $url->getHost();
$path = '//' . $host . $url->getPath();
$host = ip2long($host) ? [$host] : array_reverse(explode('.', $host));
$parts = ip2long($host) ? [$host] : array_reverse(explode('.', $host));
$re = strtr($re, [
'/%basePath%/' => preg_quote($url->getBasePath(), '#'),
'%tld%' => preg_quote($host[0], '#'),
'%domain%' => preg_quote(isset($host[1]) ? "$host[1].$host[0]" : $host[0], '#'),
'%sld%' => preg_quote(isset($host[1]) ? $host[1] : '', '#'),
'%tld%' => preg_quote($parts[0], '#'),
'%domain%' => preg_quote(isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], '#'),
'%sld%' => preg_quote(isset($parts[1]) ? $parts[1] : '', '#'),
'%host%' => preg_quote($host, '#'),
]);

} elseif ($this->type === self::RELATIVE) {
Expand Down Expand Up @@ -400,6 +401,7 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
'%tld%' => $parts[0],
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
'%sld%' => isset($parts[1]) ? $parts[1] : '',
'%host%' => $host,
]);
$url = ($this->scheme ?: $refUrl->getScheme()) . ':' . $url;
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/Routers/Route.variables.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ testRouteIn(new Route('//%sld%.%tld%/<path>', 'Default:default'), '/abc', 'Defau
], '/abc?test=testvalue');


testRouteIn(new Route('//%host%/<path>', 'Default:default'), '/abc', 'Default', [
'path' => 'abc',
'action' => 'default',
'test' => 'testvalue',
], '/abc?test=testvalue');


// alternative
testRouteIn(new Route('//example.%tld%/<path>', 'Default:default'), '/abc', 'Default', [
'path' => 'abc',
Expand Down Expand Up @@ -97,3 +104,10 @@ Assert::same('http://localhost/', $route->constructUrl($route->match($httpReques

$route = new Route('//%tld%/', 'Default:default');
Assert::same('http://localhost/', $route->constructUrl($route->match($httpRequest), $url));


// host
$url = new Nette\Http\UrlScript('http://www.example.com/');
$httpRequest = new Nette\Http\Request($url);
$route = new Route('//%host%/', 'Default:default');
Assert::same('http://www.example.com/', $route->constructUrl($route->match($httpRequest), $url));

0 comments on commit c0bcf7b

Please sign in to comment.