Leveraging API Dispatches from RPC in Apigility

Apigility provides a good resource-based API but when you need more data than a resource provides it’s time for custom RPC dispatches.

Apigility RPC provides a controller which returns an array which is transformed to JSON.

To call an API controller then modify its response you need to dispatch the controller from the RPC controller, render the HalJson resonse, then decode it back to an array.

        $hal = $this->forward()->dispatch($controller, array(
            'id' => 1,
        ));
        $renderer = $this->getServiceLocator()->get('ZFHalJsonRenderer');
        $data = json_decode($renderer->render($hal), true);

Having ran this example your $data will be an array of the rendered HalJson. You can modify it and return the modified array from the RPC controller.

        $data['moredata'] = array('more', 'data');

        return $data;

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top