我试图弄清楚如何从CLI / Scheduler上下文生成绝对的前端页面URL。我创建了以下帮助程序类:
class FrontendUrlProvider
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param ObjectManagerInterface $objectManager
* @param LoggerInterface $logger
*/
public function __construct(ObjectManagerInterface $objectManager, LoggerInterface $logger)
{
$this->objectManager = $objectManager;
$this->initializeTimeTracker();
$this->logger = $logger;
}
/**
* @param int $pageId
* @param int $languageId
* @return Uri
*/
public function pageUrl($pageId, $languageId)
{
$url = '';
$this->logger->error('generating preview link');
try {
$this->initializeTypoScriptFrontend($pageId);
$this->setUpPageDomainIfCliContext($pageId);
$contentRenderer = $this->objectManager->get(ContentObjectRenderer::class);
$command = $this->linkCommand($pageId, $languageId);
$url = $contentRenderer->typoLink_URL($command);
$this->logger->error("preview link is: $url");
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
$this->logger->error($exception->getTraceAsString());
}
return new Uri($url);
}
private function initializeTimeTracker()
{
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker();
}
}
/**
* @param int $pageId
*/
private function initializeTypoScriptFrontend($pageId)
{
if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TFSE'])) {
return;
}
$GLOBALS['TSFE'] = $this->objectManager->get(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $pageId, '');
$GLOBALS['TSFE']->sys_page = $this->objectManager->get(PageRepository::class);
$GLOBALS['TSFE']->sys_page->init(false);
$GLOBALS['TSFE']->tmpl = $this->objectManager->get(TemplateService::class);
$GLOBALS['TSFE']->tmpl->init();
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
}
/**
* @param int $pageId
*/
private function setUpPageDomainIfCliContext($pageId)
{
if (!isset($_SERVER['HTTP_HOST']) || !$_SERVER['HTTP_HOST']) {
$domainData = $GLOBALS['TSFE']->getDomainDataForPid($pageId);
if (is_array($domainData) && isset($domainData['domainName']) && !empty($domainData['domainName'])) {
$_SERVER['HTTP_HOST'] = $domainData['domainName'] ?: '';
}
}
}
/**
* @param int $pageId
* @param int $languageId
* @return array
*/
private function linkCommand($pageId, $languageId)
{
$languageQuery = http_build_query(['L' => $languageId], null, '&', PHP_QUERY_RFC3986);
return array(
'parameter' => $pageId,
'useCacheHash' => false,
'forceAbsoluteUrl' => true,
'linkAccessRestrictedPages' => true,
'additionalParams' => '&' . $languageQuery,
);
}
}
我认为只要在根行中有域记录,它就可以在TYPO3 7 LTS中正常工作。
但是,这个相同的代码段在TYPO3 8 LTS中不起作用,我需要它同时在7和8中工作。显然,我已经在v8中的根行上设置了域记录,清除了所有缓存,等等。但是我无法获得一个绝对URL。我只有相对网址。在这一点上,我对realUrl或类似的东西不感兴趣。
对于TYPO3 7,我基本上对其进行了逆向工程,但是使用TYPO3 8似乎更加复杂。您知道我该怎么做才能获得页面前端URL?
答案 0 :(得分:1)
问题似乎出在GeneralUtility
的内部缓存中,该缓存为null
的超全局变量的HTTP_HOST
缓存了一个$_SERVER
值。
因此,上述示例中的以下行无效
$_SERVER['HTTP_HOST'] = $domainData['domainName'] ?: '';
为了使其在CLI / Scheduler范围内工作,我必须通过调用清除GeneralUtility
的内部缓存
TYPO3\CMS\Core\Utility\GeneralUtility::flushInternalRuntimeCaches();
在致电$contentRenderer->typoLink_URL($command);
现在前端URL生成在TYPO3 7和8 LTS上都可以正常工作。
答案 1 :(得分:0)
https://wissen.netzhaut.de/typo3/extensionentwicklung/typolink-realurl-in-scheduler-tasks/
但实际上:我建议对自定义页面使用curl调用,该页面将提供链接(某种形式的REST API)-因为这样您就可以解决几乎所有问题-而且除了没有tsfe之外,还有其他更多问题(例如safePath \ images)。