request; $is_ajax = $request->isAjax; if ($is_ajax) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->data = ['code' => 1, 'msg' => $e->getMessage()]; \Yii::$app->response->send(); return false; } else { $this->layout = 'exception'; return $this->render('index'); // throw new UnauthorizedHttpException($e->getMessage()); } } } }