初始化仓库
This commit is contained in:
53
admin/controllers/MobileController.php
Normal file
53
admin/controllers/MobileController.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user