I made in Classes/Middleware class FormDataFetch to fetch ajax requests.
I need to get some data from repository, but I am not sure how to inject repository to middleware class. I keep getting an error "‘Call to a member function getData() on null’ in other words, my dataRepository is not injected properly.
$this->dataRepository->getData()
…
protected $dataRepository;
public function __construct(DataRepository $dataRepository)
{
$this->dataRepository = $dataRepository;
}
I tried with constructor but then it breaks my page with error
Too few arguments to function … FormDataFetch::__construct(), 0 passed in /typo3_src-11.5.36/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 3215 and exactly 1 expected
Can you post your middleware class, Services.yaml and Middlewares.php file? Maybe you did not declare your DataRepository as “public: true”? And you did clear caches?
The upper code was improvisation of real one…
I am trying to understand how to inject Repository to my Middleware class or should I put it in my Service Class.
I also needed to make Service class because I couldn’t use custom function in Controller and Middleware.
And then GPT was giving me solutions back and forward where to put my injection for Repository.
I don’t have Services.yaml in my extension. What is this thing for?
And yes, I did clear my cache, I even dump my autoload.
Hi there,
as Garvin already pointed out: Please check out the documentation for Dependency Injection, which explains the purpose of Services.yaml and what to put in there.
I think your problem here is, that there is no Services.yaml. In order to make DI work correctly, TYPO3 actually needs the Services.yaml in the Configuration directory of your extension.
To solve your problem, add the file to your extension, put some config in it (refer to the docs on how to do it) and dump autoload afterwards (or clear all caches) - then your code should work.
Another thing: According to the namespace I am assuming that you’re trying to use an Extbase repository inside a middleware. This is generally not a good idea, as Extbase needs Frontend TypoScript - which is not the case when you’re in a middleware.
It may “work” in TYPO3 11.5, but it will definitely break in TYPO3 13.4.
For more information please check out the docs:
I wonder if the answers from @ghi and @max.frerichs were of help to you. Did you manage to solve your problem? If so, please feel free to post about the solution here. It may be of help to others.