'TYPO3 11 LTS Generation of Frontend Url in REST API Context
Hi I need some support regarding frontend url generation inside building REST API. I'm using restler for the API. I could generate the url with
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($arguments['pageUid']);
return (string)$site->getRouter()->generateUri($arguments['pageUid'],$queryStrings);
But the problem is it is not building the extension parameters using the routing configuration. The url works as expected.
Update: To get more understanding: I added the info more. This is same like generating a frontend url in backend, scheduler task or command controller. Where GLOBALS['TSFE'] not available.
I use the above function like this.
public function generateUrl(
int $pageId,
array $arguments,
bool $absolute
): string
{
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId);
if (empty($site->getBase()->getHost()) || empty($site->getBase()->getScheme())) {
throw new \RuntimeException(
"Site " . $site->getIdentifier() . ' does not have proper schema or host set. Thus not usable in cli context.',
1648736865
);
}
$uri = $site
->getRouter()
->generateUri(
$pageId,
$arguments,
'',
PageRouter::ABSOLUTE_URL
);
if (empty($uri->getHost()) || empty($uri->getScheme())) {
throw new \RuntimeException(
'Build uri did not have proper schema or host set. Thus not usable in cli context. ' . (string)$uri,
1648736938
);
}
if (!$absolute) {
return $uri->getPath() . (!empty($uri->getQuery()) ? '?' . $uri->getQuery() : '');
}
return (string)$uri;
}
Any Idea ?
Solution 1:[1]
if you have PageUid already and Arguments then you can try building URI with UriBuilder of TYPO3.
TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
$uriBuilder = $renderingContext->getControllerContext()->getUriBuilder();
$uriBuilder->reset();
if ($pageUid > 0) {
$uriBuilder->setTargetPageUid($pageUid);
}
if ($pageType > 0) {
$uriBuilder->setTargetPageType($pageType);
}
if ($noCache === true) {
$uriBuilder->setNoCache($noCache);
}
if (is_string($section)) {
$uriBuilder->setSection($section);
}
if (is_string($format)) {
$uriBuilder->setFormat($format);
}
if (is_array($additionalParams)) {
$uriBuilder->setArguments($additionalParams);
}
if ($absolute === true) {
$uriBuilder->setCreateAbsoluteUri($absolute);
}
if ($addQueryString === true) {
$uriBuilder->setAddQueryString($addQueryString);
}
if (is_array($argumentsToBeExcludedFromQueryString)) {
$uriBuilder->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString);
}
if ($addQueryStringMethod !== '') {
$uriBuilder->setAddQueryStringMethod($addQueryStringMethod);
}
if ($linkAccessRestrictedPages === true) {
$uriBuilder->setLinkAccessRestrictedPages($linkAccessRestrictedPages);
}
return $uriBuilder->uriFor($action, $arguments, $controller, $extensionName, $pluginName);
Solution 2:[2]
I got my problem. The issue was with the arguments I supplied to the generate Url function. I prepared the arguments like this:
array(3) {
["tx_vshcore_profilesdetail[action]"]=>
string(6) "detail"
["tx_vshcore_profilesdetail[controller]"]=>
string(8) "Profiles"
["tx_vshcore_profilesdetail[profile]"]=>
int(1)
}
I think about the PHP function http_build_query and prepare my arguments like the above. But it should be like this.
array(1) {
["tx_vshcore_profilesdetail"]=>
array(3) {
["controller"]=>
string(8) "Profiles"
["action"]=>
string(6) "detail"
["profile_detail"]=>
int(3)
}
}
I hope it is clear where I'm wrong :).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohsin Khan |
| Solution 2 | Hoja |
