Reputation: 875
Since TYPO3 v9.4 the extbase commandController is deprecated so I use the symfony console command replacement according to: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/CommandControllers/Index.html#cli-mode
Now my FooCommandController extends from Symfony\Component\Console\Command\Command
Since I need to work with TYPO3 data I need to get them via the extbase repositories. But my whole CommandController does not have the extbase context so loading the repo via extbase dependency injection is not possible.
Also creating them manually via makeInstance or objectManager->get() is not possible since there is no objectManager available in this context.
What is the correct/non-hacky way to get access to the extbase functionallity inside a symfony commandController?
Upvotes: 4
Views: 1377
Reputation: 2921
You are on the right path already. You just need an ObjectManager instance:
use TYPO3\CMS\Extbase\Object\ObjectManager;
...
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$repo = objectManager->get(FooRepository::class);
From TYPO3 v10 we can use Symfony dependency injection (https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/DependencyInjection/Index.html).
Upvotes: 3
Reputation: 1085
The "easiest" but not recommend way would be to fetch ObjectManager
via makeInstance()
and use get()
on it.
A better way would be to separate your logic in a custom class, which uses the Dependency Injection of Extbase. You can then fetch this class the same way.
With V10 you would be able to just inject, as DI is available outside of Extbase.
Upvotes: 3