teepluss / laravel-hmvc

HMVC is a tool for making internal request.
76 stars 20 forks source link

invoke could return status and headers along with the content #18

Open malhal opened 9 years ago

malhal commented 9 years ago

Hey thought I'd let you know I had trouble with the invoke method because I wanted to see the status code and headers, and that method only returns the content inside the response. No biggie I just subclassed and reimplemented that method to return an array of those things, also renamed a few variables:

        // Dispatch request.
        $response = $this->router->dispatch($request);

        if (method_exists($response, 'getOriginalContent'))
        {
            $responseBody = $response->getOriginalContent();
        }
        else
        {
            $responseBody = $response->getContent();
        }

        // Decode json content.
        if ($response->headers->get('content-type') == 'application/json')
        {
            if (function_exists('json_decode') and is_string($responseBody))
            {
                $responseBody = json_decode($responseBody, true);
            }
        }

        // Restore the request input and route back to the original state.
        $this->request->replace($originalInput);

        $result['body'] = $responseBody;
        $result['status'] = $response->getStatusCode();
        $result['header'] = $response->headers->all();

        return $result;