public function onDispatch(MvcEvent $e) { /** @var \BedRest\Rest\Dispatcher $dispatcher */ $dispatcher = $this->getServiceLocator()->get('BedRest.Dispatcher'); $data = $dispatcher->dispatch($this->restRequest); $result = new ViewModel($data); $result->setAccept($this->restRequest->getAccept()); $e->setResult($result); return $result; }
/** * Replaces the view model with one populated with error information. * @param \Zend\Mvc\MvcEvent $e */ public function prepareExceptionViewModel(MvcEvent $e) { $error = $e->getError(); if (empty($error)) { return; } $data = array('message' => 'Error encountered'); $code = 500; switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: $data['message'] = 'Invalid resource specified'; $code = 404; break; case Application::ERROR_EXCEPTION: if ($this->displayExceptions) { /** @var \Exception $ex */ $ex = $e->getParam('exception'); $data['exception'] = array('type' => get_class($ex), 'code' => $ex->getCode(), 'message' => $ex->getMessage(), 'file' => $ex->getFile(), 'line' => $ex->getLine(), 'trace' => $ex->getTrace()); } break; } $model = new ViewModel($data); // TODO: allow the fallback content type to be specified in configuration $model->setAccept(new MediaTypeList(array('application/json'))); $e->setResult($model); /** @var \Zend\Http\Response $response */ $response = $e->getResponse(); if (!$response) { $response = new HttpResponse(); $response->setStatusCode($code); $e->setResponse($response); } else { $statusCode = $response->getStatusCode(); if ($statusCode === 200) { $response->setStatusCode($code); } } $e->setResponse($response); }