簡體   English   中英

TYPO3 如何在鈎子中注入 objectManager?

[英]TYPO3 how to inject objectManager in hook?

也許這很簡單,但我無法弄清楚。

TYPO3 8.7:我正在編寫一個小鈎子:如果滿足某個條件,我想發送一封電子郵件。 因此,我需要電子郵件模板的獨立視圖。 但是對於獨立視圖,我需要對象管理器:

/** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView
$emailView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');

在我的課程開始時,我嘗試注入 objectManager:

/**
 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
 */
protected $objectManager;

/**
 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
 * @internal
 */
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
{
    $this->objectManager = $objectManager;
}

但它不起作用:我遇到了一個錯誤:objectManager 是一個空對象。 這顯然意味着鈎子中不存在注入機制。

那如何實現呢?

Extbase 依賴注入在鈎子中不可用,因此您必須自己創建對象實例。

$standaloneView = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
->get(TYPO3\CMS\Fluid\View\StandaloneView::class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM