TYPO3 xClass和依赖注入

问题描述

我想覆盖一个Extbase控制器,例如TYPO3 v10.4中的新闻。因此,我在ext_locaconf.php中注册了XClass

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\GeorgRinger\News\Controller\NewsController::class] = [
    'className' => \Clickstorm\CsEvents\Controller\EventController::class
];

然后我要注入新的存储库

/**
 * eventDateRepository
 * 
 * @var EventDateRepository
 */
protected $eventDateRepository = null;

/**
 * Inject a eventDateRepository
 *
 * @param EventDateRepository $eventDateRepository
 */
public function injectEventDateRepository(EventDateRepository $eventDateRepository)
{
    $this->eventDateRepository = $eventDateRepository;
}

但是我的eventDateRepository为null。另外,当我改用构造函数时。

解决方法

即使我不知道这种行为的原因,一种可持续的解决方法是在initializeAction方法中手动实例化依赖项。

就我而言,这就是我写的:

/**
 * @var \TYPO3\CMS\Extbase\Service\ImageService
 */
protected $imageService;

public function initializeAction()
{
    parent::initializeAction();
    $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
    $this->imageService = $objectManager->get(\TYPO3\CMS\Extbase\Service\ImageService::class);
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...