0

tl;dr I have followed the official guide. Everything seems working except for PUT/PATCH verb. The 200 OK code is returned, but the actual model isn't updated. What can be wrong?


I have created a blank Yii 2 project that have created a REST UserController for already existing User model:

namespace app\controllers;
use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}

I have modified the model to have all fields safe:

public function rules()
{
    return [
        ['status', 'default', 'value' => self::STATUS_INACTIVE],
        ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
        [['username', 'email'], 'required'],
        [['username', 'email'], 'unique'],
        ['email', 'email'],
        [['password_hash', 'password_reset_token', 'verification_token', 'auth_key', 'status,created_at', 'updated_at', 'password'], 'safe'],
    ];
}

I have configured URL rules to have both pluralized and non-pluralized paths:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'user',
            'pluralize' => false,
            'except' => ['index'],
        ],
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'user',
            'patterns' => [
                'GET,HEAD,OPTIONS' => 'index',
            ],
        ],
    ],

I have enabled JSON input, if that matters:

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

All the verbs are processed correctly except for PATCH /users/123 / PUT /users/123. When executed in Insomnia, I am getting 200 OK, but the returned record shows no sign of modification:

enter image description here

What can be wrong or what am I missing?

trejder
  • 17,148
  • 27
  • 124
  • 216
  • 1
    Do you have maybe `updateScenario` in the controller set to something else than default? – Bizley Jun 10 '22 at 08:19
  • The _full_ controller code is given in my question in the first block. As you can see, I have nothing there what isn't mentioned in the Yii 2 Guide. – trejder Jun 10 '22 at 21:33
  • This is really weird, since I'm using it all the time with PUT and it works... – Bizley Jun 14 '22 at 06:33

0 Answers0