3

I have created rest api it's not working Could you please suggest me where i went wrong?

Error: main.CRITICAL: exception 'ReflectionException' with message 'Report ID: webapi-560e33c7e2c45; Message: Class name does not exist'

my code is:

app/etc/config.php

modules' => 
  array (  
    'Learning_Test' => 1,
  ),

app/code/Learning/Test/Api

<?php

namespace Learning\Test\Api;

interface WebserviceInterface
{

    /**
     * @param $name
     * @return mixed
     */
    public function say($name);
}

app/code/Learning/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Learning\Test\Api\WebserviceInterface" type="Learning\Test\Model\Webservice" />
</config>

app/code/Learning/etc/webapi.xml

<?xml version="1.0"?>

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">

    <!-- Example: curl http://192.168.0.52/magento-2/index.php/rest/V1/webservice/say/magento2 -->
    <route url="/V1/webservice/say/:name" method="GET">
        <service class="Learning\Test\Api\WebserviceInterface" method="say"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>

</routes>

app/code/Learning/Test/Model/Webservice.php

<?php
namespace Learning\Test\Model;
use Learning\Test\Api\WebserviceInterface;

class Webservice implements WebserviceInterface
{

    /**
     * @param $name
     * @return string
     */
    public function say($name) {
        return ' Welcome to ' . $name;
    }
}

Could you please suggest me where I went wrong?

Thanks.

  • your file name is app/code/Learning/Test/Model/Webservice.php but your class name is VLC\Test\Model\Webservice. is this a typo in the post , or this is the actual issue? – Marius Oct 2 '15 at 7:39
  • @Marius, it's Typing mistake now I have updated. – Bojjaiah Oct 2 '15 at 7:47
  • @Marius, please look my answer and confirm? – Bojjaiah Oct 2 '15 at 9:55
3

Finally I have achieved by adding below code in WebserviceInterface.php

/**
     * @param string $name of the param.
     * @return mixed|string of the param Value.
     */

    public function say($name);

I'm observing that when i have added above function with comments then it's working fine otherwise it's not working.

I don't know the comment is the key for the function to work?

If some one knows please let me know?

  • It must add type of value "int|string" ... – St3phan Jun 18 '19 at 22:29
1

Interface class is used to filter incoming and outgoing values. Based on phpdoc magento will prepare values to get from the request and pass to the model that handles it. Also response will also be filtered based on the phpdoc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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