Пример #1
0
 protected function registerViewListeners(Application $app)
 {
     // Add a listener to convert a HAL object to a response.
     $app->on(KernelEvents::VIEW, function (GetResponseForControllerResultEvent $event) use($app) {
         $result = $event->getControllerResult();
         //var_dump($event->getRequest()->attributes->all());
         if ($result instanceof Hal) {
             if (in_array($event->getRequest()->getRequestFormat(), ['json', 'hal_json'])) {
                 $response = new HalJsonResponse($result, Response::HTTP_OK);
                 $response->setPretty($app['debug']);
             } elseif ($app['debug']) {
                 // For debugging, default to returning JSON.
                 // Return application/json in dev mode because
                 // application/hal+json, while more precisely accurate,
                 // won't be rendered by most browsers.
                 $response = new Response($result->asJson(true), Response::HTTP_OK, ['Content-Type' => 'application/json']);
             } else {
                 // In production, require a proper accept header.
                 throw new NotAcceptableHttpException("Only media types application/hal+json and application/hal+xml are supported.");
             }
             $event->setResponse($response);
         }
     });
 }