typo3 11 LTS在REST API上下文中生成前端URL

发布于 2025-01-22 23:15:24 字数 1546 浏览 6 评论 0原文

嗨,我需要在构建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2025-01-29 23:15:24

如果您已经有pageuID和争论,则可以尝试使用Typo3的Uribuilder构建URI。

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);

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);
£烟消云散 2025-01-29 23:15:24

我有问题。
问题在于我提供给生成URL函数的参数。
我准备了这样的论点:

array(3) {
  ["tx_vshcore_profilesdetail[action]"]=>
  string(6) "detail"
  ["tx_vshcore_profilesdetail[controller]"]=>
  string(8) "Profiles"
  ["tx_vshcore_profilesdetail[profile]"]=>
  int(1)
}

我考虑了php函数http_build_query,并按照上面的方式准备了我的论点。但是应该这样。

array(1) {
  ["tx_vshcore_profilesdetail"]=>
  array(3) {
    ["controller"]=>
    string(8) "Profiles"
    ["action"]=>
    string(6) "detail"
    ["profile_detail"]=>
    int(3)
  }
}

我希望清楚我在哪里错了:)。  

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 :).  

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文