I made a command in TYPO3 which has arguments and dependency injection (DI). As I understood in symfony DI is made with the __construct method. But there I also have to state the argument which I want to pass to the command. So how is it done correctly?
Sources:
- symfony how DI is made: https://symfony.com/doc/current/console.html#getting-services-from-the-service-container
- Command controller in TYPO3 with symfony: https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/BackendModules/CliScripts/Index.html?highlight=command%20controller#creating-a-new-symfony-command-in-your-extension
Versions: TYPO3 9.5.5, symfony 4.2.5
Say I want to pass one argument to the command AND inject the ObjectManager from TYPO3:
<?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;
}
}
Then I call this with
bin/typo3 extension_name:someCommand foo
(where foo
is the $cliParameter
)
I get
Uncaught TYPO3 Exception Cannot instantiate interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface
So my question is: What did I wrong? What's the correct way to do this?