<?php
namespace backend\controllers\api;
use Yii;
use yii\web\Controller;
class BaseController extends Controller
{
protected function readStdIn()
{
$data = file_get_contents('php://input');
return $data;
}
protected function readStdInAsJson()
{
$data = $this->readStdIn();
$json = json_decode($data);
return $json;
}
protected function readStdInAsArray()
{
$data = $this->readStdIn();
$json = json_decode($data, true);
return $json;
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function sendResponse($data, $message = null, $exception = null)
{
\Yii::$app->response->format = 'json';
$dataStatus = [ 'code' => 0, 'error' => 'OK', 'message' => $message, 'exception' => null ];
if($exception)
{
$dataStatus = [
'code' => 1,
'error' => 'ERR_EXCEPTION',
'message' => $message,
'exception' => [
'message' => $exception->getMessage(),
'stackTrace' => $exception->getTraceAsString()
]
];
}
$outData = array_merge([
'status' => $dataStatus,
], $data);
return $outData;
}
}