Yii2 Rest API PUT方法不起作用

Jac*_*had 7 rest yii2

在我的控制器中

`namespace app\api\modules\v1\controllers;

namespace app\api\modules\v1\controllers;

use yii\rest\ActiveController;
use yii\filters\VerbFilter;
use yii\web\Response;

class CountryController extends ActiveController
{
public $modelClass = 'app\models\Country';

public function behaviors()
{
    return [
        [
           'class' => 'yii\filters\ContentNegotiator',
           'only' => ['index', 'view','create','update','search'],
           'formats' => ['application/json' =>Response::FORMAT_JSON,],

        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index'=>['get'],
                'view'=>['get'],
                'create'=>['post'],
                'update'=>['PUT'],
                'delete' => ['delete'],
                'deleteall'=>['post'],
                'search'   => ['get']
            ],

        ]
    ];
}
}`
Run Code Online (Sandbox Code Playgroud)

我试试我的POSTMAN应用程序

对于创建我使用POST 的http://本地主机/ MYAPP/API/V1 /国家工程fine.But对于更新我使用PUT HTTP://本地主机/ MYAPP/API/V1 /国家/ 16返回16的纪录JSON输出没有按预期更新.

哪里错了?谢谢!!

Pab*_*ten 5

在POSTMAN App中,打开Request body选项卡并选择x-www-form-urlencoded而不是form-data.这对我有用.

选择了x-www-form-urlencoded


Mik*_*oss -1

如果您觉得使用方便,这里还有另一个选择。相反,behaviors()您可以添加类似的内容,它将达到相同的目的,并且您不会遇到任何问题。

public function actions()
{
    $actions = parent::actions();
    unset($actions['index']);
    unset($actions['create']);
    unset($actions['delete']);
    unset($actions['update']);
    unset($actions['view']);
    return $actions;
}
Run Code Online (Sandbox Code Playgroud)