Categories


Archives


Recent Posts


Categories


Magento API Service Contracts Parse Interfaces Only

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.

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

I’m writing this down so I can remember it later – if you’re not deep into Magento service contracts apologies for the word soup.

When Magento’s parsing doc block’s for parameter types, it’s only looking at interfaces – or possibly its only looking at the type it knows about. i.e. if you have something like this

<route url="/V1/pulsestorm_apitest2/things/:id" method="GET">
    <service class="PulsestormApitest2ApiThingRepositoryInterface" method="get"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

Magento is going to look at PulsestormApitest2ApiThingRepositoryInterface for the type implementations and not the concrete class you have configuration via di.xml. In other words, if you put your @param int $id in the concrete get methods instead of the interface, Magento wont’ be able to find it will will throw a confusing “No such Class” exception when you try to call your API method.

The key to that exception message is the extra space. Magento goes looking for a type hint, assuming its a class by default (vs. a simple/scalar type like int or string)

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 4th May 2017

email hidden; JavaScript is required