M
M
mizgirek2017-03-26 15:50:16
Yii
mizgirek, 2017-03-26 15:50:16

How to get data sent by PUT method in Yii2?

Hello!
I started learning Yii2, more specifically the REST section.
I configured everything as in the documentation and almost everything works, except for requests sent by the PUT method.
The documentation says that the PUT method should update the data, but this does not happen.
I tried to get data through \Yii::$app->request->getBodyParams()but an empty array is returned. Although if you check the $_REQUEST variable in it, as expected, all the data I sent is there. For the sake of testing, I wrote the following in the beforeValidate () method:

$this->count = $_REQUEST['count '];
$this->summ = $_REQUEST['summ'];

And the data began to be updated, i.e. what I send through the PUT method began to work, but it's not right, it's dangerous and stupid, please tell me what I'm doing wrong
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            //'enableStrictParsing' => true,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'api'],
            ],
        ],

class ApiController extends ActiveController
{
    public $modelClass = 'app\models\Orders';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['corsFilter' ] = [
            'class' => \yii\filters\Cors::className(),
        ];

        $behaviors['contentNegotiator'] = [
            'class' => \yii\filters\ContentNegotiator::className(),
            'formats' => [
                'application/json' => \yii\web\Response::FORMAT_JSON,
            ],
        ];

        $behaviors['authenticator'] = [
            'class' => HttpBearerAuth::className(),
            /*'authMethods' => [
                HttpBasicAuth::className(),
                HttpBearerAuth::className(),
                QueryParamAuth::className(),
            ],*/
        ];
        
        return $behaviors;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-26
@slo_nik

Good afternoon.
Here's a webinar that explains everything.
And an example from this webinar, configuring urlManager

'rules' => [
                '' => 'site/index',
                'auth' => 'site/login',
                'POST signup' => 'site/signup',

                'GET profile' => 'profile/index',
                'PUT,PATCH profile' => 'profile/update',

                ['class' => 'yii\rest\UrlRule', 'controller' => 'post'],
            ],

All webinar examples .
PS It is desirable that you supplement your question with more detailed code, starting from the configuration and ending with the actions in the controller.
By the way, there is a restfull testing tool in phpstorm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question