Skip to content

Commit ed1ed43

Browse files
committed
- Fixed domains not being prepending properly to urls.
- Won't prepend subdomain to urls if subdomain is equal to current host. - Url: implemented parse function. - Router: getUrl now parses url to properly parse the subdomain.
1 parent a275366 commit ed1ed43

File tree

9 files changed

+55
-46
lines changed

9 files changed

+55
-46
lines changed

src/Pecee/Http/Request.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public function __construct()
130130

131131
// Check if special IIS header exist, otherwise use default.
132132
$url = $this->getHeader('unencoded-url');
133-
if($url !== null){
133+
if ($url !== null) {
134134
$this->setUrl(new Url($url));
135-
}else{
135+
} else {
136136
$this->setUrl(new Url(urldecode((string)$this->getHeader('request-uri'))));
137137
}
138138
$this->setContentType((string)$this->getHeader('content-type'));
@@ -225,7 +225,7 @@ public function getHeaders(): array
225225
public function getIp(bool $safeMode = false): ?string
226226
{
227227
$headers = [];
228-
if($safeMode === false) {
228+
if ($safeMode === false) {
229229
$headers = [
230230
'http-cf-connecting-ip',
231231
'http-client-ip',
@@ -303,9 +303,9 @@ public function getHeader(string $name, $defaultValue = null, bool $tryParse = t
303303
*/
304304
public function getFirstHeader(array $headers, $defaultValue = null)
305305
{
306-
foreach($headers as $header) {
306+
foreach ($headers as $header) {
307307
$header = $this->getHeader($header);
308-
if($header !== null) {
308+
if ($header !== null) {
309309
return $header;
310310
}
311311
}
@@ -329,7 +329,7 @@ public function getContentType(): ?string
329329
*/
330330
protected function setContentType(string $contentType): self
331331
{
332-
if(strpos($contentType, ';') > 0) {
332+
if (strpos($contentType, ';') > 0) {
333333
$this->contentType = strtolower(substr($contentType, 0, strpos($contentType, ';')));
334334
} else {
335335
$this->contentType = strtolower($contentType);
@@ -371,7 +371,7 @@ public function isAjax(): bool
371371

372372
/**
373373
* Returns true when request-method is type that could contain data in the page body.
374-
*
374+
*
375375
* @return bool
376376
*/
377377
public function isPostBack(): bool
@@ -395,11 +395,11 @@ public function setUrl(Url $url): void
395395
{
396396
$this->url = $url;
397397

398-
if ($this->url->getHost() === null) {
398+
if ($this->url->getHost() === null && $this->getHost() !== null) {
399399
$this->url->setHost((string)$this->getHost());
400400
}
401401

402-
if($this->isSecure() === true) {
402+
if ($this->isSecure() === true) {
403403
$this->url->setScheme('https');
404404
}
405405
}

src/Pecee/Http/Url.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ class Url implements JsonSerializable
6767
public function __construct(?string $url)
6868
{
6969
$this->originalUrl = $url;
70+
$this->parse($url, true);
71+
}
7072

73+
public function parse(?string $url, bool $setOriginalPath = false): self
74+
{
7175
if ($url !== null && $url !== '/') {
7276
$data = $this->parseUrl($url);
7377

@@ -79,7 +83,10 @@ public function __construct(?string $url)
7983

8084
if (isset($data['path']) === true) {
8185
$this->setPath($data['path']);
82-
$this->originalPath = $data['path'];
86+
87+
if ($setOriginalPath === true) {
88+
$this->originalPath = $data['path'];
89+
}
8390
}
8491

8592
$this->fragment = $data['fragment'] ?? null;
@@ -88,6 +95,7 @@ public function __construct(?string $url)
8895
$this->setQueryString($data['query']);
8996
}
9097
}
98+
return $this;
9199
}
92100

93101
/**

src/Pecee/SimpleRouter/Route/LoadableRoute.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Pecee\Http\Request;
77
use Pecee\SimpleRouter\Exceptions\HttpException;
88
use Pecee\SimpleRouter\Router;
9+
use Pecee\SimpleRouter\SimpleRouter;
910

1011
abstract class LoadableRoute extends Route implements ILoadableRoute
1112
{
@@ -138,12 +139,6 @@ public function findUrl(?string $method = null, $parameters = null, ?string $nam
138139
{
139140
$url = $this->getUrl();
140141

141-
$group = $this->getGroup();
142-
143-
if ($group !== null && count($group->getDomains()) !== 0) {
144-
$url = '//' . $group->getDomains()[0] . $url;
145-
}
146-
147142
/* Create the param string - {parameter} */
148143
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
149144

@@ -177,7 +172,15 @@ public function findUrl(?string $method = null, $parameters = null, ?string $nam
177172
}
178173
}
179174

180-
return rtrim('/' . ltrim($url, '/'), '/') . '/';
175+
$url = rtrim('/' . ltrim($url, '/'), '/') . '/';
176+
177+
$group = $this->getGroup();
178+
179+
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
180+
$url = '//' . $group->getDomains()[0] . $url;
181+
}
182+
183+
return $url;
181184
}
182185

183186
/**

src/Pecee/SimpleRouter/Route/RouteController.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pecee\SimpleRouter\Route;
44

55
use Pecee\Http\Request;
6+
use Pecee\SimpleRouter\SimpleRouter;
67

78
class RouteController extends LoadableRoute implements IControllerRoute
89
{
@@ -77,13 +78,15 @@ public function findUrl(?string $method = null, $parameters = null, ?string $nam
7778

7879
$group = $this->getGroup();
7980

80-
if ($group !== null && count($group->getDomains()) !== 0) {
81-
$url .= '//' . $group->getDomains()[0];
82-
}
83-
8481
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower((string)$method) . implode('/', $parameters);
8582

86-
return '/' . trim($url, '/') . '/';
83+
$url = '/' . trim($url, '/') . '/';
84+
85+
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
86+
$url = '//' . $group->getDomains()[0] . $url;
87+
}
88+
89+
return $url;
8790
}
8891

8992
public function matchRoute(string $url, Request $request): bool

src/Pecee/SimpleRouter/Route/RouteGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function setSettings(array $settings, bool $merge = false): IRoute
220220
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
221221
}
222222

223-
if ($merge === false && isset($settings['domain']) === true) {
223+
if (isset($settings['domain']) === true) {
224224
$this->setDomains((array)$settings['domain']);
225225
}
226226

src/Pecee/SimpleRouter/Route/RouteResource.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pecee\SimpleRouter\Route;
44

55
use Pecee\Http\Request;
6+
use Pecee\SimpleRouter\SimpleRouter;
67

78
class RouteResource extends LoadableRoute implements IControllerRoute
89
{
@@ -80,7 +81,14 @@ public function findUrl(?string $method = null, $parameters = null, ?string $nam
8081
return rtrim($this->url . $parametersUrl . $this->urls[$url], '/') . '/';
8182
}
8283

83-
return $this->url . $parametersUrl;
84+
$url = $this->url . $parametersUrl;
85+
86+
$group = $this->getGroup();
87+
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
88+
$url = '//' . $group->getDomains()[0] . $url;
89+
}
90+
91+
return $url;
8492
}
8593

8694
protected function call($method): bool

src/Pecee/SimpleRouter/Router.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -691,21 +691,15 @@ public function getUrl(?string $name = null, $parameters = null, ?array $getPara
691691

692692
/* If nothing is defined and a route is loaded we use that */
693693
if ($name === null && $loadedRoute !== null) {
694-
return $this->request
695-
->getUrlCopy()
696-
->setPath($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))
697-
->setParams($getParams);
694+
return $this->request->getUrlCopy()->parse($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))->setParams($getParams);
698695
}
699696

700697
if ($name !== null) {
701698
/* We try to find a match on the given name */
702699
$route = $this->findRoute($name);
703700

704701
if ($route !== null) {
705-
return $this->request
706-
->getUrlCopy()
707-
->setPath($route->findUrl($route->getMethod(), $parameters, $name))
708-
->setParams($getParams);
702+
return $this->request->getUrlCopy()->parse($route->findUrl($route->getMethod(), $parameters, $name))->setParams($getParams);
709703
}
710704
}
711705

@@ -720,18 +714,12 @@ public function getUrl(?string $name = null, $parameters = null, ?array $getPara
720714

721715
/* Check if the route contains the name/alias */
722716
if ($processedRoute->hasName($controller) === true) {
723-
return $this->request
724-
->getUrlCopy()
725-
->setPath($processedRoute->findUrl($method, $parameters, $name))
726-
->setParams($getParams);
717+
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
727718
}
728719

729720
/* Check if the route controller is equal to the name */
730721
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
731-
return $this->request
732-
->getUrlCopy()
733-
->setPath($processedRoute->findUrl($method, $parameters, $name))
734-
->setParams($getParams);
722+
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
735723
}
736724

737725
}
@@ -741,10 +729,7 @@ public function getUrl(?string $name = null, $parameters = null, ?array $getPara
741729
$url = trim(implode('/', array_merge((array)$name, (array)$parameters)), '/');
742730
$url = (($url === '') ? '/' : '/' . $url . '/');
743731

744-
return $this->request
745-
->getUrlCopy()
746-
->setPath($url)
747-
->setParams($getParams);
732+
return $this->request->getUrlCopy()->parse($url)->setParams($getParams);
748733
}
749734

750735
/**
@@ -974,4 +959,4 @@ public function addExceptionHandler(IExceptionHandler $handler): self
974959
return $this;
975960
}
976961

977-
}
962+
}

tests/Pecee/SimpleRouter/RouterUrlTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ public function testOptionalParameters()
9494

9595
public function testSimilarUrls()
9696
{
97+
TestRouter::reset();
9798
// Match normal route on alias
9899
TestRouter::get('/url11', 'DummyController@method1');
99100
TestRouter::get('/url22', 'DummyController@method2');
100101
TestRouter::get('/url33', 'DummyController@method2')->name('match');
101102

103+
102104
TestRouter::debugNoReset('/url33', 'get');
103105

104106
$this->assertEquals(TestRouter::getUrl('match'), TestRouter::getUrl());

tests/TestRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function debugNoReset(string $testUrl, string $testMethod = 'get')
1717
{
1818
$request = static::request();
1919

20-
$request->setUrl((new \Pecee\Http\Url($testUrl))->setHost('local.unitTest'));
20+
$request->setUrl((new \Pecee\Http\Url($testUrl)));
2121
$request->setMethod($testMethod);
2222

2323
static::start();

0 commit comments

Comments
 (0)