Skip to content

Commit 7d2581c

Browse files
committed
Route: added port to %host%, %domain%, %tld% WIP [Closes #10][Closes nette/application#297]
1 parent c96e9e7 commit 7d2581c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Routing/Route.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,13 @@ public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?stri
272272
$parts = ip2long($host)
273273
? [$host]
274274
: array_reverse(explode('.', $host));
275+
$port = $refUrl->getDefaultPort() === ($tmp = $refUrl->getPort()) ? '' : ':' . $tmp;
275276
$url = strtr($url, [
276277
'/%basePath%/' => $refUrl->getBasePath(),
277-
'%tld%' => $parts[0],
278-
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
278+
'%tld%' => $parts[0] . $port,
279+
'%domain%' => (isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0]) . $port,
279280
'%sld%' => $parts[1] ?? '',
280-
'%host%' => $host,
281+
'%host%' => $host . $port,
281282
]);
282283
}
283284

tests/Route/ports.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Routing\Route ports
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Http\UrlScript;
10+
use Nette\Routing\Route;
11+
use Tester\Assert;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
17+
$route = new Route('//%domain%');
18+
19+
$url = $route->constructUrl(
20+
[],
21+
new UrlScript('https://example.org:8000'),
22+
);
23+
Assert::same('https://example.org:8000', $url);
24+
25+
$url = $route->constructUrl(
26+
[],
27+
new UrlScript('https://localhost:8000'),
28+
);
29+
Assert::same('https://localhost:8000', $url);

0 commit comments

Comments
 (0)