10

In my controller:

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']
            ],

        ]
    ];
}
}`

I try from my POSTMAN App

For create I use POST http://localhost/myapp/api/v1/countries Works fine.But For Update I use PUT http://localhost/myapp/api/v1/countries/16 it returns 16 the record as JSON output not updating as expected.

What was wrong? Thanks!!

1
  • If the PUT call returns the correct object it seems like the UpdateAction is working fine. Are you sure the values you are posting are in your rules() set? Commented May 4, 2016 at 15:56

2 Answers 2

8

In POSTMAN App, open the Request body tab and select x-www-form-urlencoded instead of form-data. That worked for me.

x-www-form-urlencoded selected

Sign up to request clarification or add additional context in comments.

Comments

-1

Here is another option if you feel comfortable using it. Instead of behaviors() you can add something like this and it will serve the same purpose and you wont have any problem.

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

1 Comment

answer not related to the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.