You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 13后端非Controller类如何获取TypoScript?

TYPO3 13后端获取TypoScript的替代方案

问题背景

TYPO3 13中,原后端获取TypoScript的两种方式均失效:BackendConfigurationManager被声明为final类无法设置页面ID,TemplateService已被废弃。官方虽不建议后端使用TypoScript,但实际场景(如后端链接处理器)需要复用前端已配置的TypoScript常量,避免重复配置冗余风险。

推荐解决方案

方案1:模拟前端上下文获取TypoScript常量

通过初始化TypoScriptFrontendController(TSFE)来获取完整TypoScript配置,适合必须复用现有TypoScript常量的场景。需注意上下文隔离,避免影响其他后端操作:

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

class YourBackendClass
{
    public function __construct(
        private readonly SiteFinder $siteFinder,
        private readonly Context $context
    ) {}

    public function getTypoScriptConstants(int $pageUid): array
    {
        $site = $this->siteFinder->getSiteByPageId($pageUid);
        $languageId = $this->context->getPropertyFromAspect('language', 'id', 0);
        $language = $site->getLanguageById($languageId);
        
        // 暂存原有TSFE(如果存在)
        $originalTsfe = $GLOBALS['TSFE'] ?? null;
        try {
            // 初始化新的TSFE实例
            $GLOBALS['TSFE'] = TypoScriptFrontendController::create(
                $this->context,
                $site,
                $language,
                $pageUid
            );
            $GLOBALS['TSFE']->initTemplate();
            $GLOBALS['TSFE']->getConfigArray();
            
            // 返回解析后的常量配置
            return $GLOBALS['TSFE']->tmpl->setup['constants.'] ?? [];
        } finally {
            // 恢复原有TSFE状态
            if ($originalTsfe !== null) {
                $GLOBALS['TSFE'] = $originalTsfe;
            } else {
                unset($GLOBALS['TSFE']);
            }
        }
    }
}

方案2:迁移至站点配置(TYPO3 13推荐)

将配置项移至站点级配置文件(config/sites/{站点标识}/config.yaml),符合TYPO3最新架构设计,支持多站点/多语言差异化配置:

settings:
  your_extension:
    detail_page_uid: 123 # 编辑配置的详情页UID

后端类中通过SiteFinder获取配置:

use TYPO3\CMS\Core\Site\SiteFinder;

class YourBackendClass
{
    public function __construct(private readonly SiteFinder $siteFinder) {}

    public function getDetailPageUid(int $pageUid): int
    {
        $site = $this->siteFinder->getSiteByPageId($pageUid);
        return (int)($site->getConfiguration()['settings']['your_extension']['detail_page_uid'] ?? 0);
    }
}

方案3:使用扩展全局配置

若配置为全局统一值,可在扩展的Configuration/ExtensionConfiguration.php中定义配置项,让编辑在后端“设置”模块中配置:

return [
    'detailPageUid' => [
        'label' => '详情页UID',
        'description' => '外部记录详情页的UID',
        'type' => 'input',
        'eval' => 'int',
        'default' => 0,
    ],
];

后端类中通过ExtensionConfiguration获取:

use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;

class YourBackendClass
{
    public function __construct(private readonly ExtensionConfiguration $extensionConfiguration) {}

    public function getDetailPageUid(): int
    {
        return (int)$this->extensionConfiguration->get('你的扩展密钥', 'detailPageUid');
    }
}

方案选择建议

  • 需多站点/多语言差异化配置:优先选方案2,符合TYPO3官方设计方向;
  • 必须复用现有TypoScript常量:临时使用方案1,注意上下文隔离;
  • 全局统一配置:选方案3,配置流程更简单。

内容的提问来源于stack exchange,提问作者dasoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:16:09