Answer the question
In order to leave comments, you need to log in
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'];
'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
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'],
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question