Пример #1
0
 * However, by parsing the request querystring's 'type' paramter, it is easy to install
 * different response type handlers.  Below is an alternate csv handler.
 */
$app->after(function () use($app) {
    // OPTIONS have no body, send the headers, exit
    if ($app->request->getMethod() == 'OPTIONS') {
        $app->response->setStatusCode('200', 'OK');
        $app->response->send();
        return;
    }
    // Respond by default as JSON
    if (!$app->request->get('format') || $app->request->get('format') == 'json') {
        // Results returned from the route's controller.  All Controllers should return an array
        $records = $app->getReturnedValue();
        $response = new \Api\Responses\JSONResponse();
        $response->useEnvelope(true)->convertSnakeCase(false)->send($records);
        return;
    } else {
        if ($app->request->get('format') == 'javascript') {
            // Results returned from the route's controller.  All Controllers should return an array
            $records = $app->getReturnedValue();
            $response = new \Api\Responses\JavascriptResponse();
            $response->useEnvelope(true)->convertSnakeCase(false)->send($records);
            return;
        } else {
            if ($app->request->get('format') == 'csv') {
                $records = $app->getReturnedValue();
                $response = new \Api\Responses\CSVResponse();
                $response->useHeaderRow(true)->send($records);
                return;
            } else {