G
G
good_beginer2019-03-11 11:44:04
Yii
good_beginer, 2019-03-11 11:44:04

How to configure yii2 api correctly?

<?php

namespace api\modules\v1\controllers;

use yii;
use yii\rest\ActiveController;

/**
 * Default controller for the `v1` module
 */
class ActionsController extends ActiveController
{
    public $modelClass = 'api\modules\v1\models\Test';

    public function actionHistory()
    {
        $request = Yii::$app->request;
        if ($request->get('format') == 'json')
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    }

    public function actionHistorySum()
    {
        $request = Yii::$app->request;
        if ($request->get('format') == 'json')
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    }

}

I am studying api based on yii2 and ran into a problem, if you follow the link to this controller, it works immediately and gives the result in the form of xml. (because in the model *tableName(){ return 'dbo.Schema' } * ) . How to make it not show up?
schema api->modules->v1->controllers,models

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
devilsD, 2019-03-12
@devilsD

Try

if ($request->get('format') == 'json')
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

Render in beforeAction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question