0

I have no issues when doing an around plugin - but for a before and after plugin, I always get errors:

For instance, I want to print out something before or after a REST call is made:

class Rest
{

 /**
  * Handle REST request
  *
  * @param \Magento\Framework\App\RequestInterface|\Magento\Framework\Webapi\Rest\Request $request
  */
   public function beforeDispatch(\Magento\Framework\App\RequestInterface $request)
   {
       var_dump($request->getContent());
   }

}

<type name="Magento\Webapi\Controller\Rest">
    <plugin name="restPlugin" type="MyCompany\MyMod\Plugin\Rest" sortOrder="1" disabled="false"/>
</type>

I get the following.

'Uncaught TypeError: Argument 1 passed to MyCompany\MyMod\Plugin\Rest::beforeDispatch() must implement interface Magento\Framework\App\RequestInterface, instance of Magento\Webapi\Controller\Rest\Interceptor given

It's the same if I do after - I've tried to cast the input variable differently, but doesn't work.

2

You class should be


class Rest
{
    /**
     * Handle REST request
     *
     * @param \Magento\Webapi\Controller\Rest $subject
     * @param \Magento\Framework\App\RequestInterface|\Magento\Framework\Webapi\Rest\Request $request
     */
    public function beforeDispatch(
        \Magento\Webapi\Controller\Rest $subject,
        \Magento\Framework\App\RequestInterface $request
    ) {
        error_log('Inside Rest');
    }
}

Clear cache and delete var/generation/*

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.