Last active
August 29, 2019 20:55
-
-
Save fcaldarelli/51f3d65d8eaf9827f54b12aba9912881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment