Yii2: JsonParse does not work by default

1

The point is that when accessing my controller Entrenamiento through the browser (URL), it returns XML format unless you specify in the request Content-type: application/json . According to the documentation this should work by default (JsonParse) when setting it in the configuration file.

I leave the configuration in my file frontend/config/main.php :

$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'entrenamiento'],

                '<controller:\w+>/' => '<controller>/index',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            ],
        ],
        'request' => [
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
];

In my controller ( frontend/controllers/EntrenamientoController.php ) I have:

namespace frontend\controllers;

use yii\rest\ActiveController;

/**
* Entrenamiento controller
*/
class EntrenamientoController extends ActiveController
{
    public $modelClass = 'common\models\Entrenamiento';
}
    
asked by Chofoteddy 05.12.2015 в 20:35
source

1 answer

1

Some examples to return in json format from a controller can be in these ways:

Directly from an action

use Yii;
use yii\web\Response;

...

public function actionIndex()
{
    Yii::$app->response->format = Response::FORMAT_JSON;
}

If there are more actions that need to return in json format configure your controller in this way

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        [
            'class' => ContentNegotiator::className(),
            'only' => ['index', 'view']
            'formats' => [
                'application/json' => Response::FORMAT_JSON,
            ],
        ],
    ];
}

If it is a RESTful API, it can be done from the general configuration of Yii

'contentNegotiator' => [
    'class' => ContentNegotiator::className(),
    'formats' => [
        'application/json' => Response::FORMAT_JSON,
        'application/xml' => Response::FORMAT_XML,
    ],
],
    
answered by 13.02.2016 в 00:09