[$this, 'beforeSend'], ]; } public function beforeSend() { $this->_response = Yii::$app->getResponse(); if( !$this->negotiate ){ $this->_response->format = $this->format; }else { $this->negotiate(); } } private function negotiate() { $acceptTypes = Yii::$app->getRequest()->getAcceptableContentTypes(); $acceptTypes = array_keys($acceptTypes); foreach ($acceptTypes as $acceptType){ switch ($acceptType) { case "text/plain": $this->_response->format = Response::FORMAT_RAW; break; case "application/html": case "text/html": case "*/*": $this->_response->format = $this->format; break; case "application/json": case "text/json": $this->_response->format = Response::FORMAT_JSON; break; case "application/xml": case "text/xml": $this->_response->format = Response::FORMAT_XML; break; } } } }