2

http://localhost:8888/rest/all/V1/api/orders/?from=31.05.2020&to=31.06.2020

My webapi xml file is:

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/api/orders/" method="GET">
        <service class="Lovat\Api\Api\OrdersRepositoryInterface"
                 method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Is it possible to add from and to parameters to this route?

1 Answer 1

3

Yes it's possible.

Note - to make them required you can keep the "force" attribute true, else false.

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/api/orders/" method="GET">
        <service class="Lovat\Api\Api\OrdersRepositoryInterface"
                 method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
        <data>
            <parameter name="from" force="true">%from%</parameter>
            <parameter name="to" force="false">%to%</parameter>
        </data>
    </route>
</routes>

And your Interface should be like this

/**
 * @param string $from
 * @param string|null $to
 * @return string
 */
public function get($from, $to = null);

Notice that I've kept $to as null because we have not made it required.

11
  • i have this errror "message": "Class from does not exist", "code": -1, Commented Jul 23, 2020 at 14:21
  • can you share your Lovat\Api\Api\OrdersRepositoryInterface as well Commented Jul 23, 2020 at 14:40
  • I've also updated my answer for changes in Interface file Commented Jul 23, 2020 at 14:45
  • @AfreenSheikh, nicely explained. Commented Jul 23, 2020 at 14:46
  • 1
    @HiteshBalpande Thanks :) Commented Jul 23, 2020 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.