Files
xk-api-yii/admin/controllers/MobileController.php
2024-09-19 11:32:39 +08:00

54 lines
1.3 KiB
PHP

<?php
namespace admin\controllers;
use common\components\ApiErrorHandler;
use yii\rest\Controller;
class MobileController extends Controller
{
public function afterAction($action, $result)
{
$rs = parent::afterAction($action, $result);
// 也可以再定义 response
// $response = Yii::$app->response;
// $response->statusCode = 200;
// $response->data = ['message' => 'hello world'];
return ['data' => $rs, 'error' => '0'];
}
public function init()
{
parent::init();
$handler = new ApiErrorHandler();
\Yii::$app->set('errorHandler', $handler);
$handler->register();
}
public function formatDataBeforeSend($event){
$response = $event->sender;
//自已定义失败的返回
if ($response->data !== null && $response->isSuccessful==false) {
$response->data = [
'code' => 500,
'status' => "FAIL" ,
'message' => "系统繁忙,请稍后再试",
];
$response->statusCode = 200;
}
}
public function behaviors()
{
$behaviors = parent::behaviors();
unset($behaviors['contentNegotiator']['formats']['application/xml']);//去除xml格式就按剩下的json显示
return $behaviors;
}
}