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');
在我的 class 开始时,我尝试注入对象管理器:
/**
* @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);
也许只是简单,但我想不通
TYPO3 8.7:我正在编写一个小钩子:如果满足特定条件我想发送电子邮件。因此我需要电子邮件模板的独立视图。但是对于独立视图,我需要对象管理器:
/** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView
$emailView = $this->objectManager->get('TYPO3\CMS\Fluid\View\StandaloneView');
在我的 class 开始时,我尝试注入对象管理器:
/**
* @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);