docs
docs copied to clipboard
Adds an example of modifying items by listening to the POST_READ event
I was trying to modify an API response to add a dynamically generated image URL to any Image items returned from the API. I tried to modify the example BookSubscriber to do this, but struggled to find out how to obtain the item(s) that would be returned by the API from a read request. @TimoBakx helped me out, and this PR adds an example of how to do this.
Hi, and thanks for contributing to improve the docs, this kind of changes are very appreciated. As for #622, don't you think a normalizer decorator would be a better place to this? Then the code will not be coupled to the HTTP layer anymore, and will be reusable, for instance if you want to send the document to a worker using the Messenger component, or to another internal service using Kafka or a RabbitMQ bus. WDYT?
Thanks for the feedback. As to #622 @TimoBakx would have to answer that.
I agree with @dunglas on this. A decorator would work better for most cases. The only case in which a POST_READ listener would be a better option is if you need the data during other parts of handling the request (other listeners further down the line or a controller in case of a custom action). In which case, the question is if you shouldn't put the logic in that spot in the first place.
That being said, I have yet to rewrite and test my part of it to use the decorator instead of a listener.