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';
}