AD0-E724 · Question #50
A developer needs to declare an observer that listens for the 'catalog_product_save_before' event. In which XML file must this observer be registered?
The correct answer is A. 'etc/events.xml'. In Magento 2, observers are registered in etc/events.xml (or etc/frontend/events.xml / etc/adminhtml/events.xml for area-specific scope), which is the dedicated configuration file that maps event names to observer classes - making A correct. etc/config.xml is for module default…
Question
A developer needs to declare an observer that listens for the 'catalog_product_save_before' event. In which XML file must this observer be registered?
Options
- A'etc/events.xml'
- B'etc/config.xml'
- C'etc/module.xml'
- D'etc/di.xml'
How the community answered
(42 responses)- A71% (30)
- B5% (2)
- C17% (7)
- D7% (3)
Explanation
In Magento 2, observers are registered in etc/events.xml (or etc/frontend/events.xml / etc/adminhtml/events.xml for area-specific scope), which is the dedicated configuration file that maps event names to observer classes - making A correct. etc/config.xml is for module default configuration values (store settings, flags), not event wiring. etc/module.xml only declares the module's name and setup version for the framework to recognize it. etc/di.xml handles dependency injection - plugins, preferences, and virtual types - not event observers.
Memory tip: Think "Events go in Events.xml" - the file name matches the concept exactly, unlike the other files which serve entirely different purposes.
Topics
Community Discussion
No community discussion yet for this question.