/** * @throws Exception */ public function init() { parent::init(); if (\Yii::$app->user) { \Yii::$app->user->enableSession = false; \Yii::$app->user->loginUrl = null; } if (!YII_DEBUG) { $this->controllerMap = []; } Yii::setAlias('@api', __DIR__ . DIRECTORY_SEPARATOR); /** @noinspection PhpUndefinedFieldInspection */ if (YII_DEBUG || Yii::$app->has('api') && Yii::$app->api->enableDocs) { $this->controllerMap['doc'] = 'vr\\api\\controllers\\DocController'; } $this->set('harvester', new Harvester()); Yii::$app->set('request', ['enableCookieValidation' => false, 'enableCsrfValidation' => false, 'class' => Request::className(), 'parsers' => ['application/json' => 'yii\\web\\JsonParser']]); Yii::$app->set('response', ['class' => '\\yii\\web\\Response', 'on beforeSend' => function ($event) { $response = $event->sender; if ($response->format == Response::FORMAT_JSON) { if (!$response->data) { $response->data = []; } if ($response->isSuccessful) { $response->data = ['success' => $response->isSuccessful] + $response->data; } else { $response->data = ['success' => $response->isSuccessful, 'exception' => $response->data]; } } }, 'formatters' => [Response::FORMAT_JSON => ['class' => '\\vr\\api\\components\\JsonResponseFormatter', 'prettyPrint' => YII_DEBUG, 'encodeOptions' => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]]]); }
/** * Resets Yii2 Request component so it can handle another fake request and resolves it */ private function resolve() { Yii::$app->set('request', ['class' => \yii\web\Request::className(), 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq', 'scriptFile' => __DIR__ . '/index.php', 'scriptUrl' => '/index.php']); return Yii::$app->request->resolve(); }