initTempFilePaths(); parent::init(); $this->request = Yii::$app->request; $this->response = Yii::$app->response; } /* @inheritdoc */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ], ], ], ]; } /** * 渲染 JSON 数据结果 * * @param $data * @return string */ public function renderJson($data) { return json_encode($data); } /** * 初始化web目录文件夹, 完成自动创建 */ private function initTempFilePaths() { $webRoot = Yii::getAlias('@webroot'); // 临时文件 $tmpFilePath = $webRoot . '/tmp'; if (!is_dir($tmpFilePath)) { mkdir($tmpFilePath, 0777, true); } // 上传文件 $uploadFilePath = $webRoot . '/upload'; if (!is_dir($uploadFilePath)) { mkdir($uploadFilePath, 0777, true); } // 下载文件 $downloadFilePath = $webRoot . '/download'; if (!is_dir($downloadFilePath)) { mkdir($downloadFilePath, 0777, true); } } /** * 过滤空格 * @param $var * @return mixed */ public function filterVar($var) { $var = str_replace(" ", "", $var); $var = str_replace("\t", "", $var); return $var; } }