如何在调度程序命令中生成前端 URI (TYPO3 9) [英] how to generate Frontend URI in Scheduler Command (TYPO3 9)

查看:20
本文介绍了如何在调度程序命令中生成前端 URI (TYPO3 9)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TYPO3 v9 中的调度程序命令中生成前端 URI 的最佳方法是什么.

What is the best way to generate frontend URIs in a scheduler command in TYPO3 v9.

我见过手动初始化 TSFE 的尝试,但对我来说这似乎很可疑.还有其他方法吗?

I have seen attempts by initializing the TSFE manually, but for me this seems fishy. Are there any other ways?

推荐答案

在任何上下文 (FE/BE/CLI) 中创建链接的正确方法是使用 PageRouter.此路由器始终附加到 站点,因此您需要先检索正确的站点,例如通过使用 SiteFinder.之后,您可以使用 PageRouter::generateUri().

The proper way to create links in any context (FE/BE/CLI) is by using the PageRouter. This router is always attached to a site, so you will need to retrieve the correct site first, e.g. by using the SiteFinder. After that you can use PageRouter::generateUri().

完整示例:

use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageUid);
$arguments = [
    'foo' => 1,
];
$uri = (string)$site->getRouter()->generateUri((string)$pageUid, $arguments);

请注意,此 API 对 Extbase 一无所知,并通过 $arguments 传递给 URI,因此如果您需要模仿 Extbase UriBuilder 的行为,您需要自己做:

Notice that this API knows nothing about Extbase and passes through $arguments to the URI so if you need to mimic the behavior of the Extbase UriBuilder you'll need to do that yourself:

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ExtensionService;

$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$extensionService = $objectManager->get(ExtensionService::class);
$argumentsPrefix = $extensionService->getPluginNamespace($extensionName, $pluginName);
$arguments = [
    $argumentsPrefix => [
      'action' => $actionName,
      'controller' => $controllerName,
    ],
];

这篇关于如何在调度程序命令中生成前端 URI (TYPO3 9)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆