Skip to content

Commit 3147004

Browse files
committed
added website getUrl & frontendModel getAbsoluteFrontendUrl functions
1 parent 05c7eb3 commit 3147004

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

app/base/abstracts/Models/FrontendModel.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use App\Base\Models\Block;
2424
use Exception;
2525
use Throwable;
26+
use App\Base\GraphQl\GraphQLExport;
2627

2728
/**
2829
* A model that will be shown on frontend
@@ -115,4 +116,22 @@ public function getContent() : string
115116
{
116117
return true;
117118
}
119+
120+
/**
121+
* gets full frontend url for object
122+
*
123+
* @return string
124+
* @throws Exception
125+
*/
126+
#[GraphQLExport]
127+
public function getAbsoluteFrontendUrl(): string
128+
{
129+
$this->checkLoaded();
130+
131+
if (($this->getUrl() ?? $this->getUrlKey()) === null) {
132+
return "";
133+
}
134+
135+
return $this->getWebsite()->getUrl() . $this->getFrontendUrl();
136+
}
118137
}

app/base/models/Website.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,29 @@ public function postPersist(array $persistOptions = []): BaseModel
8383

8484
return parent::postPersist($persistOptions);
8585
}
86+
87+
/**
88+
* Returns website URL
89+
*
90+
* @return string
91+
*/
92+
#[GraphQLExport()]
93+
public function getUrl() : string
94+
{
95+
$parsed = parse_url(
96+
sprintf(
97+
"%s://%s%s%s",
98+
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http',
99+
(is_numeric($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) ? ':' . $_SERVER['SERVER_PORT'] : '',
100+
$this->getDomain(),
101+
'/'
102+
)
103+
);
104+
105+
if ($parsed) {
106+
return $parsed['scheme'] . '://' . $parsed['host'];
107+
}
108+
109+
return 'https://'.$this->getDomain();
110+
}
86111
}

0 commit comments

Comments
 (0)