Skip to content

Commit ca7b8aa

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

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"require": {
1818
"php": ">=7.2 <8.3",
19-
"nette/http": "^3.0",
19+
"nette/http": "^3.2",
2020
"nette/utils": "^3.2 || ~4.0.0"
2121
},
2222
"require-dev": {

src/Routing/Route.php

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

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)