Categories


Archives


Recent Posts


Categories


Magento 2: Dependency Injection and API Calls

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This one came out of a conversation in my Patreon mentoring Slack room. Someone was having trouble with their Magento 2 object manager configurations (di.xml) not showing up — but only during checkout. Their problem? They were dropping their di.xml files into the frontend folder

etc/frontend/di.xml

This is good enough to get class preferences and plugins set for the page that renders the checkout, as well as any pages that make ajax requests through a frontend controller. However — the stock Magento checkout application also accesses Magento’s rest API directly, and rest API requests happen in a custom area call webapi_rest. This means di.xml configuration needs to go into

etc/webapi_rest/di.xml

if Magento’s making a rest request via javascript/ajax and you want your code to run during that request.

Copyright © Alan Storm 1975 – 2017 All Rights Reserved

Originally Posted: 14th November 2017