Skip to content

Commit ab1ed67

Browse files
davidkrmeladg
authored andcommitted
Route: add %host% variable (#140)
1 parent 80da58e commit ab1ed67

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/Application/Routers/Route.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ public function match(Nette\Http\IRequest $httpRequest)
150150
if ($this->type === self::HOST) {
151151
$host = $url->getHost();
152152
$path = '//' . $host . $url->getPath();
153-
$host = ip2long($host) ? array($host) : array_reverse(explode('.', $host));
153+
$parts = ip2long($host) ? array($host) : array_reverse(explode('.', $host));
154154
$re = strtr($re, array(
155155
'/%basePath%/' => preg_quote($url->getBasePath(), '#'),
156-
'%tld%' => preg_quote($host[0], '#'),
157-
'%domain%' => preg_quote(isset($host[1]) ? "$host[1].$host[0]" : $host[0], '#'),
158-
'%sld%' => preg_quote(isset($host[1]) ? $host[1] : '', '#'),
156+
'%tld%' => preg_quote($parts[0], '#'),
157+
'%domain%' => preg_quote(isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0], '#'),
158+
'%sld%' => preg_quote(isset($parts[1]) ? $parts[1] : '', '#'),
159+
'%host%' => preg_quote($host, '#'),
159160
));
160161

161162
} elseif ($this->type === self::RELATIVE) {
@@ -381,25 +382,25 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
381382
} while (TRUE);
382383

383384

384-
if ($this->type !== self::HOST) {
385+
if ($this->type === self::HOST) {
386+
$host = $refUrl->getHost();
387+
$parts = ip2long($host) ? array($host) : array_reverse(explode('.', $host));
388+
$url = strtr($url, array(
389+
'/%basePath%/' => $refUrl->getBasePath(),
390+
'%tld%' => $parts[0],
391+
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
392+
'%sld%' => isset($parts[1]) ? $parts[1] : '',
393+
'%host%' => $host,
394+
));
395+
$url = ($this->flags & self::SECURED ? 'https:' : 'http:') . $url;
396+
} else {
385397
if ($this->lastRefUrl !== $refUrl) {
386398
$scheme = ($this->flags & self::SECURED ? 'https://' : 'http://');
387399
$basePath = ($this->type === self::RELATIVE ? $refUrl->getBasePath() : '');
388400
$this->lastBaseUrl = $scheme . $refUrl->getAuthority() . $basePath;
389401
$this->lastRefUrl = $refUrl;
390402
}
391403
$url = $this->lastBaseUrl . $url;
392-
393-
} else {
394-
$host = $refUrl->getHost();
395-
$host = ip2long($host) ? array($host) : array_reverse(explode('.', $host));
396-
$url = strtr($url, array(
397-
'/%basePath%/' => $refUrl->getBasePath(),
398-
'%tld%' => $host[0],
399-
'%domain%' => isset($host[1]) ? "$host[1].$host[0]" : $host[0],
400-
'%sld%' => isset($host[1]) ? $host[1] : '',
401-
));
402-
$url = ($this->flags & self::SECURED ? 'https:' : 'http:') . $url;
403404
}
404405

405406
if (strpos($url, '//', 7) !== FALSE) {

tests/Routers/Route.variables.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ testRouteIn(new Route('//%sld%.%tld%/<path>', 'Default:default'), '/abc', 'Defau
5555
), '/abc?test=testvalue');
5656

5757

58+
testRouteIn(new Route('//%host%/<path>', 'Default:default'), '/abc', 'Default', array(
59+
'path' => 'abc',
60+
'action' => 'default',
61+
'test' => 'testvalue',
62+
), '/abc?test=testvalue');
63+
64+
5865
// alternative
5966
testRouteIn(new Route('//example.%tld%/<path>', 'Default:default'), '/abc', 'Default', array(
6067
'path' => 'abc',
@@ -97,3 +104,10 @@ Assert::same('http://localhost/', $route->constructUrl($route->match($httpReques
97104

98105
$route = new Route('//%tld%/', 'Default:default');
99106
Assert::same('http://localhost/', $route->constructUrl($route->match($httpRequest), $url));
107+
108+
109+
// host
110+
$url = new Nette\Http\UrlScript('http://www.example.com/');
111+
$httpRequest = new Nette\Http\Request($url);
112+
$route = new Route('//%host%/', 'Default:default');
113+
Assert::same('http://www.example.com/', $route->constructUrl($route->match($httpRequest), $url));

0 commit comments

Comments
 (0)