Example #1
0
        $app->response->setStatusCode('200', 'OK');
        $app->response->send();
        return;
    }
    // Respond by default as JSON
    if (!$app->request->get('type') || $app->request->get('type') == 'json') {
        // Results returned from the route's controller.  All Controllers should return an array
        $records = $app->getReturnedValue();
        $response = new \Base\Framework\Responses\JSONResponse();
        $response->useEnvelope(true)->convertSnakeCase(false)->send($records);
        return;
    } else {
        if ($app->request->get('type') == 'csv') {
            $records = $app->getReturnedValue();
            $response = new \Base\Framework\Responses\CSVResponse();
            $response->useHeaderRow(true)->send($records);
            return;
        } else {
            throw new \Base\Framework\Exceptions\HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand type specified by type paramter in query string.', 'internalCode' => 'NF1000', 'more' => 'Type may not be implemented. Choose either "csv" or "json"'));
        }
    }
});
/**
* The notFound service is the default handler function that runs when no route was matched.
* We set a 404 here unless there's a suppress error codes.
*/
$app->notFound(function () use($app) {
    throw new \Base\Framework\Exceptions\HTTPException('Not Found.', 404, array('dev' => 'That route was not found on the server.', 'internalCode' => 'NF1000', 'more' => 'Check route for mispellings.'));
});
/**
* If the application throws an HTTPException, send it on to the client as json.