Typo3 12 Context API - 用户方面获取自定义 fe_user 属性

问题描述 投票:0回答:1

我的 fe_user 表具有不同的自定义属性。现在我想通过 Context API 访问这些属性,如下所示:

$context = GeneralUtility::makeInstance(Context::class);
$context->getPropertyFromAspect('frontend.user', 'xxxx');

由于 TYPO3\CMS\Core\Context\UserAspect 中的实现,这不起作用

public function get(string $name)
    {
        switch ($name) {
            case 'id':
                return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
            case 'username':
                return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
            case 'isLoggedIn':
                return $this->isLoggedIn();
            case 'isAdmin':
                return $this->isAdmin();
            case 'groupIds':
                return $this->getGroupIds();
            case 'groupNames':
                return $this->getGroupNames();
        }
        throw new AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
    }

除了通过旧方式之外,我如何访问这些属性

$GLOBALS['TSFE']->fe_user->user['xxx']

typo3 typo3-12.x
1个回答
0
投票

我认为有以下三种方法:

1.)
获取用户的 ID:

$context = GeneralUtility::makeInstance(Context::class);
$userId = $context->getPropertyFromAspect('frontend.user', 'xxxx');

1a.)
然后使用 Querybuilder 访问 fe_user 表并获取您需要的数据。

1b.)
为前端用户实现您自己的模型和存储库,并通过存储库获取数据。

2.)
实现扩展 UserAspect 类的 CustomUserAspect 类。现在您可以将 CustomUserAspect 添加到 Context 并以这种方式获取数据。

我想我会尽可能长时间地使用旧方法,然后更喜欢解决方案 1a。

© www.soinside.com 2019 - 2024. All rights reserved.