Skip to content

Magento 2 Plugin After Execute Controller

I have an existing controller that returns a JSON.

public function execute()
{    

    $customer = $this->_customerModel->getById(175);
    $resultJson = $this->resultFactory->create(MagentoFrameworkControllerResultFactory::TYPE_JSON);
    $resultJson->setData(
        [
            'status' => 'ok',
            'message' => 'Success.',
            'data' => $customer->__toArray()
        ]
        );

    return $resultJson;
    
}

I have created afterExecute() plugin, In which I’m trying to get the data of the result.

public function afterExecute(TestEndpoint $subject, $result)
{
    $result2 = json_decode($result);
    return $result;
}

There is an error throwing which is saying that $result is an Object not a JSON string.

I want to get the customer data that was returned to the controller.

How can I accomplish that?