Elgg\Ajax\Service::respondFromApiResponse PHP Method

respondFromApiResponse() public method

Send a JSON HTTP response based on the given API response
public respondFromApiResponse ( Elgg\Services\AjaxResponse $api_response, string $hook_type = '' ) : Symfony\Component\HttpFoundation\JsonResponse
$api_response Elgg\Services\AjaxResponse API response
$hook_type string The hook type. If given, the response will be filtered by hook
return Symfony\Component\HttpFoundation\JsonResponse
    public function respondFromApiResponse(AjaxResponse $api_response, $hook_type = '')
    {
        $api_response = $this->filterApiResponse($api_response, $hook_type);
        $response = $this->buildHttpResponse($api_response);
        $this->response_sent = true;
        return _elgg_services()->responseFactory->send($response);
    }

Usage Example

Example #1
0
 public function testCanNotSendANewResponseAfterAjaxResponseFromApiResponseIsSent()
 {
     $service = $this->createService();
     $api_response = new \Elgg\Ajax\Response();
     $api_response->setData((object) ['value' => 'foo']);
     ob_start();
     $json_response = $this->ajax->respondFromApiResponse($api_response);
     ob_get_clean();
     $this->assertEquals($json_response, $service->send($service->prepareResponse('bar')));
 }