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

如何在TYPO3认证服务中正确获取TypoScript配置?

在TYPO3认证服务中正确获取前端TypoScript配置的方法

针对你遇到的问题——前端认证服务注入ConfigurationManagerInterface却拿到BackendConfigurationManager,核心原因是认证服务执行时机较早,$GLOBALS['TYPO3_REQUEST']尚未初始化,导致ConfigurationManager自动回退到后端实例。以下是几种可行的解决方式:

1. 手动实例化并配置FrontendConfigurationManager

直接创建FrontendConfigurationManager实例,手动传入所需依赖(站点、语言、请求对象),强制使用前端上下文:

use TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Http\ServerRequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

// 通过构造函数注入SiteFinder
public function __construct(
    private readonly SiteFinder $siteFinder
) {}

public function getOpenIdConfig(): array
{
    // 获取当前站点(根据页面ID匹配,可根据实际场景调整)
    $site = $this->siteFinder->getSiteByPageId($GLOBALS['TSFE']->id ?? 0);
    $siteLanguage = $site->getDefaultLanguage();
    
    // 生成或复用前端请求对象
    $request = $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals();
    $request = $request->withAttribute('site', $site)
                       ->withAttribute('language', $siteLanguage);

    // 初始化FrontendConfigurationManager并设置上下文
    $frontendConfigManager = GeneralUtility::makeInstance(FrontendConfigurationManager::class);
    $frontendConfigManager->setRequest($request);
    $frontendConfigManager->setSite($site);
    $frontendConfigManager->setSiteLanguage($siteLanguage);

    // 获取插件TypoScript配置(替换为你的插件签名,如tx_yourextension_pluginname)
    $typoScriptConfig = $frontendConfigManager->getConfiguration(
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
        'yourextension',
        'pluginname'
    );

    return $typoScriptConfig;
}

2. 通过Site对象直接读取TypoScript配置

利用TYPO3的Site API获取站点完整TypoScript配置,再通过TypoScriptService解析为数组:

use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Site\SiteFinder;

// 通过构造函数注入依赖
public function __construct(
    private readonly SiteFinder $siteFinder,
    private readonly TypoScriptService $typoScriptService
) {}

public function getOpenIdConfig(): array
{
    // 获取当前站点
    $site = $this->siteFinder->getSiteByPageId($GLOBALS['TSFE']->id ?? 0);
    // 提取站点TypoScript配置树
    $typoScriptTree = $site->getConfiguration()['config']['typoScriptSetup'] ?? [];
    // 转换为扁平化数组
    $parsedTypoScript = $this->typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptTree);
    
    // 提取插件配置(对应TypoScript路径:plugin.tx_yourextension.settings.)
    return $parsedTypoScript['plugin']['tx_yourextension']['settings'] ?? [];
}

3. 迁移配置到站点配置(推荐替代方案)

将OpenID相关配置(客户端ID、密钥、well-known URL)放到站点配置的自定义字段中,直接通过Site对象读取,无需依赖TypoScript:

use TYPO3\CMS\Core\Site\SiteFinder;

// 通过构造函数注入SiteFinder
public function __construct(
    private readonly SiteFinder $siteFinder
) {}

public function getOpenIdConfig(): array
{
    $site = $this->siteFinder->getSiteByPageId($GLOBALS['TSFE']->id ?? 0);
    $siteConfig = $site->getConfiguration();
    
    return [
        'clientId' => $siteConfig['openid_client_id'] ?? '',
        'clientSecret' => $siteConfig['openid_client_secret'] ?? '',
        'wellKnownUrl' => $siteConfig['openid_well_known_url'] ?? ''
    ];
}

注意事项

  • 认证服务类需确保依赖通过DI容器注入,避免手动实例化非单例类。
  • 若$GLOBALS['TSFE']未初始化,可通过请求域名匹配站点:$site = $this->siteFinder->getSiteByRequest($request);,前提是能获取有效请求对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:50:32