Reputation: 315
I'm an extremely novice programmer who finds himself tasked with learning how to program for Magento. So please forgive me for such a rudimentary question but there don't seem to be a ton of beginning level content on Google regarding Mage.
Can someone explain to me what exactly an observer is? What does it do? What can it be used for?
If someone can give me a super 101 explanation (not assuming much prior knowledge) you'll be my new hero. Thanks.
Upvotes: 3
Views: 372
Reputation: 10898
You can consider Event observer as a trigger.
Once you have set an event observer, for example you can set observer before
or after
an event, i.e., You can add a event which would execute right after user adds a product to cart or before the add product to cart.
In this event observer, you can write code to customize the data which is either passed to the occuring event (before) or is the output of occured event (after)
Event :
In Magento you may consider a Controller Action as an event, for example addAction in CartController is an event.
Observer :
As mentioned in name, the observer observes when this action occurs (in our case addAction in CartController) and calls a function either before or after this addAction is called. You may add your custom code in this obeserver for customization.
Upvotes: 3
Reputation: 14440
An Observer is the piece of code you'll need to write if you implement an Event.
Your question is : what is an event ?
See an event as a "broadcast action" that you can intercept in order to add your specific code to a specific action. There are events fired all over magento key functionnalities.. For example, you can intercept: - after or before saving a product - product added to the cart - etc
It's just an open-door leaved by magento core developers for you to plug-in..
In magento you have several ways to modify the behavior of the standard fucntionnality : - you can rewrite classes ( tags in config.xml) - and you can use the events when an event is available for the functionnality you want to modify
To understand, dive in the code and search "dispatchEvent" in app/code/core ...
Upvotes: 1