Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Dependency Injection Bugs in Extbase?

Tags:

typo3

extbase

I'm building an extension in Extbase (latest version, latest typo3) and am having repositories injected into my models.

This simply does not work. No errors, no clues, nothing. The inject* Method simply does not get called. The exact same Injection works in the controller.

Is it possible to inject Repositories into models in Extbase? In general, injection to models is possible since 1.4.

How can I debug this? Where do I have to look?

like image 289
shredding Avatar asked Dec 05 '25 09:12

shredding


2 Answers

This is a common misconception:

Not every class you use in TYPO3 CMS uses dependency injection by default - and it's a good thing.

What is true, is that every object that has been instantiated by the ObjectManager can benefit from it.

In short: if you new Thing() some object, it won't benefit from dependency injection. If you use the ObjectManager to get an instance of something, the whole dependency injection gallore will rain down on your new instance:

  • constructor injection [Example: How can I use an injected object in the constructor?
  • annotations are read and field injections are done
  • setter injection was done in the past (Remark: I think it's deprecated)
  • public method (if existent) initializeObject is called

Note that injected objects are being instantiated by the objectManager as well-so recursion is possible if injected ServiceA needs an injected ServiceB.

Singletons are possible as well if you implement the marker interface SingletonInterface.

like image 112
Cedric Ziel Avatar answered Dec 09 '25 00:12

Cedric Ziel


Dependency injection only works if you get an instance of the object via the ObjectManager. If you are using the good ol'

t3lib_div::makeInstance('Tx_yourextension_domain_model_thing')

inject* methods are not being called.

There is a german blog entry explaining how it works.

like image 32
konsolenfreddy Avatar answered Dec 09 '25 01:12

konsolenfreddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!