private function giveUserFile($filename) { $var = new Response(); $var->sendFile(Yii::$app->params['uploadPath'] . $filename); $var->send(); unlink(Yii::$app->params['uploadPath'] . $filename); }
/** * Send the result. */ public function send() { //PS: I think this is a bit wrong. But this works, for now. $this->_transport->open(); $this->_processor->process($this->_protocol, $this->_protocol); $this->_transport->close(); parent::send(); }
/** * Uploads the file and update database */ public function run() { $file = UploadedFile::getInstanceByName('attachment[file]'); $result = $this->insertMedia($file); $response = new Response(); $response->setStatusCode(200); $response->format = Response::FORMAT_JSON; if ($result['success'] === true) { $response->setStatusCode(200); $response->data = ['file' => ['url' => '/' . $result['data']['source'], 'media_id' => $result['data']['id']]]; $response->send(); } else { $response->statusText = $result['message']; $response->setStatusCode(500); $response->send(); Yii::$app->end(); } }
public function send() { if ($this->finishRequestAfterSend && function_exists('fastcgi_finish_request')) { $this->on(static::EVENT_AFTER_SEND, 'fastcgi_finish_request', null, false); } //如果是DEBUG if (YII_DEBUG) { $this->getHeaders()->add('Request-Duration', (microtime(true) - YII_BEGIN_TIME) * 1000000); } return parent::send(); }
/** @inheritdoc */ protected function renderException($exception) { $event = new ExceptionEvent($exception); $this->trigger(self::EVENT_BEFORE_EXCEPTION, $event); if (\Yii::$app->has('response')) { $response = \Yii::$app->getResponse(); } else { $response = new Response(); } $response->data = $this->convertExceptionToArray($exception); $response->setStatusCode(property_exists($exception, 'statusCode') ? $exception->statusCode : 500); $response->send(); }
/** * Renders the exception. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { if (!$exception instanceof JqException) { parent::renderException($exception); return; } if (\Yii::$app->has('response')) { $response = \Yii::$app->getResponse(); } else { $response = new Response(); } $response->setStatusCode(500); $response->data['message'] = $exception->getMessage(); $response->send(); }
/** * Override send() method. * * $this->data member contains data released to client. */ public function send() { $responseMessage = ApiResponseCode::responseMessageFromCode($this->statusResponseCode); if ($this->isClientError) { $dataOut = $this->data; if ($this->statusCode == 401) { // Not authorized $dataOut = null; $this->fillStatusResponse(ApiResponseCode::ERR_LOGIN_REQUIRED); } else { if ($this->statusCode == 404) { // Non found $dataOut = null; $this->fillStatusResponse(ApiResponseCode::ERR_METHOD_NOT_FOUND); } } $this->data = ['status' => ['response_code' => $this->statusResponseCode, 'response_message' => $this->statusResponseMessage, 'response_extra' => $this->statusResponseExtra], 'data' => $dataOut]; } else { $this->data = ['status' => ['response_code' => $this->statusResponseCode, 'response_message' => $responseMessage, 'response_extra' => $this->statusResponseExtra], 'data' => $this->data]; } parent::send(); }
/** * Renders the exception. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { //如果存在未提交的事务,则对事务进行回滚 $transaction = Yii::$app->db->getTransaction(); if ($transaction) { $transaction->rollback(); } //对返回内容进行渲染 if (Yii::$app->has('response')) { $response = Yii::$app->getResponse(); // reset parameters of response to avoid interference with partially created response data // in case the error occurred while sending the response. $response->isSent = false; $response->stream = null; $response->data = null; $response->content = null; } else { $response = new Response(); } $useErrorView = $response->format === Response::FORMAT_HTML && !YII_DEBUG; //如果是用户定义的异常,则需要将异常错误信息抛出,如果是接口类型的并且有model类型的错误 if ($useErrorView) { if ($this->errorAction !== null) { $result = Yii::$app->runAction($this->errorAction); if ($result instanceof Response) { $result = $result; } else { $result->data = $result; } } else { //在没有默认异常处理action的情况下,直接渲染文件 $file = $useErrorView ? $this->errorView : $this->exceptionView; $responseData = $this->renderFile($file, ['exception' => $exception]); } } else { if ($response->format === Response::FORMAT_JSON || $response->format === Response::FORMAT_XML) { $response->data = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; if ($exception instanceof LBUserException) { $response->data['errors'] = $exception->getErrors(); } } else { $response->data = $this->convertExceptionToArray($exception); } } //调试状态状态码为500, 非调试状态不抛出异常 if (!YII_DEBUG || $exception instanceof LBUserException) { $response->setStatusCode(200); } else { $response->setStatusCode(500); } $response->send(); }
/** * Renders the exception. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { //如果存在未提交的事务,则对事务进行回滚 $transaction = Yii::$app->db->getTransaction(); if ($transaction) { $transaction->rollback(); } //对返回内容进行渲染 if (Yii::$app->has('response')) { $response = Yii::$app->getResponse(); // reset parameters of response to avoid interference with partially created response data // in case the error occurred while sending the response. $response->isSent = false; $response->stream = null; $response->data = null; $response->content = null; } else { $response = new Response(); } $useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException); //如果是用户定义的异常,则需要将异常错误信息抛出,如果是接口类型的并且有model类型的错误 //需要使用用户定制的errorView,同时用户在配置文件中指定了errorAction if ($useErrorView && $this->errorAction !== null) { $result = Yii::$app->runAction($this->errorAction); if ($result instanceof Response) { $result = $result; } else { $response->data = $result; } } elseif ($response->format === Response::FORMAT_HTML) { if (YII_ENV_TEST || isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') { $response->data = '<pre>' . $this->htmlEncode(static::convertExceptionToString($exception)) . '</pre>'; } else { if (YII_DEBUG) { ini_set('display_errors', 1); } $file = $useErrorView ? $this->errorView : $this->exceptionView; $response->data = $this->renderFile($file, ['exception' => $exception]); } } elseif ($response->format === Response::FORMAT_RAW) { $response->data = static::convertExceptionToString($exception); } elseif ($response->format === Response::FORMAT_JSON || $response->format === Response::FORMAT_XML) { $response->data = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; if ($exception instanceof LBUserException) { $response->data['errors'] = $exception->getErrors(); } } else { $response->data = $this->convertExceptionToArray($exception); } //调试状态状态码为500, 非调试状态不抛出异常 if (!YII_DEBUG || $exception instanceof LBUserException) { $response->setStatusCode(200); } else { $response->setStatusCode(500); } $response->send(); }
/** * Action for file uploads via sir-trevor image block from SirTrevorWidget (input widget) */ public function actionRedactorUpload() { //header('Access-Control-Allow-Origin: *'); /** * @var \kato\modules\media\Media $module */ $module = \Yii::$app->controller->module; $result = $module->mediaUpload(); $response = new Response(); $response->format = Response::FORMAT_JSON; if ($result['success'] === true) { if ($this->doMediaJoin($result)) { //success $response->setStatusCode(200); $response->data = ['filelink' => '/' . $result['data']['source']]; $response->send(); } else { $response->statusText = 'Unable to do join of media with content.'; $response->send(); Yii::$app->end(); } } else { $response->statusText = $result['message']; $response->setStatusCode(500); $response->send(); Yii::$app->end(); } }
/** * @inheritdoc */ public function send() { if ($this->isSent) { return; } $this->addSafetyHeaders(); parent::send(); }
/** * @inheritdoc */ public function renderException($exception) { if (Yii::$app->has('response')) { $response = Yii::$app->getResponse(); // reset parameters of response to avoid interference with partially created response data // in case the error occurred while sending the response. $response->isSent = false; $response->stream = null; $response->data = null; $response->content = null; } else { $response = new Response(); } $data = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; if ($exception instanceof ValidateException) { $data['fields'] = $exception->getErrors(); } elseif ($exception instanceof BadRequestHttpException) { $trace = $exception->getTrace(); $trace = $trace[0]; if (isset($trace['class']) && isset($trace['function'])) { if ($trace['class'] === 'yii\\web\\JsonParser' && $trace['function'] === 'parse') { $data['code'] = 3900; } elseif ($trace['class'] === 'yii\\web\\Controller' && $trace['function'] == 'beforeAction') { $data['code'] = 3901; } elseif ($trace['class'] === 'yii\\web\\Controller' && $trace['function'] == 'bindActionParams') { $data['code'] = 3902; } } } elseif ($exception instanceof NotFoundHttpException) { $trace = $exception->getTrace(); $trace = $trace[0]; if ($trace['class'] === 'yii\\web\\Request' && $trace['function'] === 'resolve') { $data['code'] = 3910; } elseif ($trace['class'] === 'yii\\web\\Application' && $trace['function'] === 'handleRequest') { $data['code'] = 3910; } } elseif ($exception instanceof UnauthorizedHttpException) { if ($exception->getCode() == 0) { $data['code'] = 1001; } } elseif ($exception instanceof ForbiddenHttpException) { if ($exception->getCode() == 0) { $data['code'] = 1013; } } elseif ($exception instanceof HttpException) { if ($exception->getName() == 'OAuth2') { $map = ['required_token' => 1001, 'invalid_token' => 1002, 'expired_token' => 1003, 'invalid_client' => 1004, 'invalid_grant' => 1005, 'invalid_request' => 1006, 'invalid_uri' => 1007, 'invalid_scope' => 1008, 'redirect_uri_mismatch' => 1009, 'insufficient_scope' => 1010, 'unauthorized_client' => 1011, 'malformed_token' => 1012]; $data['code'] = isset($map[$exception->errorCode]) ? $map[$exception->errorCode] : 1099; } } if ($exception instanceof HttpException) { $response->setStatusCode($exception->statusCode); } else { $response->setStatusCode(500); $data['message'] = Response::$httpStatuses[500]; if (YII_DEBUG) { $data['trace'] = explode("\n", $exception->__toString()); } } $response->format = Response::FORMAT_JSON; $response->data = $data; $response->send(); }