【发布时间】:2019-04-03 07:53:33
【问题描述】:
我在 TYPO3 中创建了一个带有参数和依赖注入 (DI) 的命令。 正如我在 symfony 中所理解的,DI 是使用 __construct 方法制作的。但是在那里我还必须说明我想传递给命令的参数。那么它是如何正确完成的呢?
来源:
- symfony 如何制作 DI:https://symfony.com/doc/current/console.html#getting-services-from-the-service-container
- 带有 symfony 的 TYPO3 中的命令控制器:https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/BackendModules/CliScripts/Index.html?highlight=command%20controller#creating-a-new-symfony-command-in-your-extension
版本: TYPO3 9.5.5、symfony 4.2.5
假设我想将一个参数传递给命令并从 TYPO3 注入 ObjectManager:
<?php
namespace Vendor\ExtensionName\Command;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use Symfony\Component\Console\Command\Command;
class SomeCommand extends Command
{
/**
* Object Manager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
*/
protected $objectManager;
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
*/
public function __construct(
string $cliParameter,
\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
{
$this->cliParameter = $cliParameter;
$this->objectManager = $objectManager;
}
}
然后我用
调用它bin/typo3 extension_name:someCommand foo
(其中foo 是$cliParameter)
我明白了
Uncaught TYPO3 Exception Cannot instantiate interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface
所以我的问题是:我做错了什么?这样做的正确方法是什么?
【问题讨论】:
-
什么是 symfony 版本?
-
您是否尝试过注入 ObjectManager 而不是 ObjectManagerInterface? (是的,不赞成依赖特定的实现,但也许typo3 DI不够聪明......?)