typo3 11 LTS在REST API上下文中生成前端URL
嗨,我需要在构建REST API内部的前端URL生成的支持。我正在使用restler作为API。 我可以使用该URL生成URL,
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($arguments['pageUid']);
return (string)$site->getRouter()->generateUri($arguments['pageUid'],$queryStrings);
但问题是它不是使用路由配置构建扩展参数。 URL按预期工作。
更新: 为了获得更多了解:我添加了更多信息。 这与在后端,调度程序任务或命令控制器中生成前端URL一样。 Globals ['tsfe']不可用的地方。
我使用上述功能。
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;
}
有什么想法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您已经有pageuID和争论,则可以尝试使用Typo3的Uribuilder构建URI。
if you have PageUid already and Arguments then you can try building URI with UriBuilder of TYPO3.
我有问题。
问题在于我提供给生成URL函数的参数。
我准备了这样的论点:
我考虑了php函数http_build_query,并按照上面的方式准备了我的论点。但是应该这样。
我希望清楚我在哪里错了:)。  
I got my problem.
The issue was with the arguments I supplied to the generate Url function.
I prepared the arguments like this:
I think about the PHP function http_build_query and prepare my arguments like the above. But it should be like this.
I hope it is clear where I'm wrong :).