J
J
jacksparrow2015-05-13 21:53:03
Yii
jacksparrow, 2015-05-13 21:53:03

Rest Api on yii2 in Json format?

I decided to make within the site, the controller responsible for the rest of the action.
How can I reduce all answers to one format that would simply return data and without unnecessary information, but within one controller. Current way

public function behaviors()
    {
       $behaviors = parent::behaviors();
       $behaviors['authenticator'] = [
            'class' => QueryParamAuth::className(),
       ];
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

I got this result, which is redundant information for the user
{"name":"Unauthorized","message":"You are requesting with an invalid credential.","code":0,"status":401,"type":"yii\\web\\UnauthorizedHttpException"}

How can I, within only one controller, fully control the output of errors, but not touch the rest of the project?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Terminaft, 2015-05-13
@Terminaft

Try to put YII_DEBUG = false in the index file.
You can still try to do as it is written here , but this is for all controllers at once.

A
Airat Khisyamov, 2015-12-23
@airatkh

public $serializer ='frontend\modules\v2\component\CustomSerializer',

    public function actionSpecialFoApi()
    {

     if(rest_responce == true) {
        /** @var \frontend\modules\v2\component\ApicoSerializer $serializer */
        return Yii::createObject($this->serializer)->serialize($data);
        return; //возможно это может надо???
    }
}

It says
yii\rest\Serializer
Serializes the given data into a format that can be easily turned into other formats.
You may override this method to support more object types.
The code has not been tested, sorry)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question