/** * Return routes required by ControllerProviderInterface * * @param Application $app * * @return ControllerCollection */ public function connect(Application $app) { $controllers = $app['controllers_factory']; // controller level exception handler $exceptionHandler = function ($exception) use($app) { // CollectionValidatorException will be handled automatically for ALL controllers if ($exception instanceof CollectionValidatorException) { throw new HttpException($exception->getCode(), $exception->getMessage(), $exception); } if ($exception instanceof AssertException) { // rethrow this exception to let the app handle it throw $exception; } // 1. if we've made it here, just let the main exception handler handle this // 2. $app->abort() is just a short cut to throw an HttpException. // Here we also pass in the previous exception such that we can log all errors from the main exception handler. throw new HttpException(500, '', $exception); }; /* Description: Root of the api with a very long description and newlines that shouldn't break the generated code */ // GET / $controllers->get('/', function (Request $request) use($app, $exceptionHandler) { try { $parameters = array(); $validParameters = self::validateRequestParameters($parameters, $request); $data = \Controllers\HomeController::index(); if ($data instanceof Response) { return $data; } } catch (\Exception $exception) { // action level exception handler // controller level exception handler $exceptionHandler($exception); } // respond in json for now...probably shouldn't handle it from here $headers = array('Content-Type' => 'application/json'); return new Response(json_encode($data), 200, $headers); })->bind('home'); /* Description: Root of the api */ // POST /post $controllers->post('/post', function (Request $request) use($app, $exceptionHandler) { try { $parameters = array(); $validParameters = self::validateRequestParameters($parameters, $request); $data = \Controllers\HomeController::posted(); if ($data instanceof Response) { return $data; } } catch (\Exception $exception) { // action level exception handler // controller level exception handler $exceptionHandler($exception); } // respond in json for now...probably shouldn't handle it from here $headers = array('Content-Type' => 'application/json'); return new Response(json_encode($data), 200, $headers); })->bind('home.post'); return $controllers; }