Exemplo n.º 1
0
 /**
  * After action.
  *
  * @param \CInlineAction $action Action from controller
  *
  * @return \Docolight\Http\Response
  */
 public function afterAction($action)
 {
     parent::afterAction($action);
     // Basic data template
     $statusCode = 200;
     $data = array('status' => $statusCode, 'message' => 'Success', 'value' => $this->data);
     // Let's find an error
     if ($this->error instanceof Exception) {
         // throw $this->error;
         // Basic data template for an exception
         $statusCode = 500;
         $data = ['status' => $statusCode, 'message' => 'Error', 'value' => ['code' => $this->error->getCode(), 'message' => $this->error->getMessage()]];
         // If exception code is an HTTP resoponse code
         if ($message = Response::getMessageForCode($this->error->getCode())) {
             $statusCode = $this->error->getCode();
             $data['status'] = $statusCode;
             $data['message'] = preg_replace('/^\\d+ /', '', $message);
         } else {
             if (YII_DEBUG) {
                 $data['value']['stack_trace'] = $this->error->getTrace();
             }
         }
     } elseif ($this->data instanceof Response) {
         return $this->data->send();
     }
     return response('json', $statusCode, collect($data), $this->headers)->send();
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function finalize()
 {
     list($status, $headers, $body) = parent::finalize();
     $this->stringRepresentation = $this->convertToStringRepresentation($body);
     $this->length = strlen($this->stringRepresentation);
     $this->headers->set('Content-Type', $this->getContentType());
     $this->headers->set('Content-Length', $this->length);
     return array($status, $headers, $body);
 }