Rest API failing with behaviours

Hi, i’m trying to get a rest api using 2.0. I have the default actions on a controller working fine but I’m tying to extend the actions I can do. I think that as well as having a new action I must also use behaviours to set what is possible with each new action, so I have added this to my controller.

public function behaviors()


{


    return [


        'verbs' => [


            'class' => VerbFilter::className(),


            'actions' => [


                'index'  => ['get'],


                'view'   => ['get'],


                'create' => ['get', 'post'],


                'update' => ['get', 'put', 'post'],


                'delete' => ['post', 'delete'],


            ],


        ],


    ];


} 

However, the causes the existing actions to fail with

Invalid Parameter – yii\base\InvalidParamException

Response content must not be an array.

Any ideas what I have done wrong?

OK if I add the behaviour to the controller it returns an array with each model instance a new array item. This causes an error in Response.php

array(20) (

[0] => array(3) (

If I remove behaviours then it returns the array formatted as a string

(string) <?xml version="1.0" encoding="UTF-8"?>

<response><item>

Any idea why?!

So looking at the Rest/ActiveController I suspect all I need to add to is override this instead of the whole of the behaviours property

protected function verbs()


{


    return [


        'index' =&gt; ['GET', 'HEAD'],


        'view' =&gt; ['GET', 'HEAD'],


        'create' =&gt; ['POST'],


        'update' =&gt; ['PUT', 'PATCH'],


        'delete' =&gt; ['DELETE'],


    ];


}

However, I still can’t understand how to create new actions for my api?

This works for existing actions http://localhost/mysite/frontend/web/apicontroller

But i can’t get a new action to work. I assumed i would create a new action then then be able to access it via

http://localhost/mysite/frontend/web/apicontroller/newAction

Solution @ stackoverflow:

Thanks, I had seen that but was still having issues. I had something like this

‘extraPatterns’ => [

            'GET /dosomething' =&gt; 'dosomething', // ' refers to 'actionDosomething' '


        ],

Stepping through the urlmanager it became apparent that the leading slash was not needed as it created a rule that contained ‘//’ Changing it to this then worked fine.

‘extraPatterns’ => [

            'GET dosomething' =&gt; 'dosomething',


        ]

Your problem is that you have overridden the behaviors() method completely. The parent classes have behaviors attached that negotiate the content headers and response format. To get round this your behaviors needs to return an array merged with the parent behaviors. So your behaviors() method will look like this;


public function behaviors() {

    return ArrayHelper::merge(parent::behaviors(), [

                'verbs' => [

                    'class' => VerbFilter::className(),

                    'actions' => [

                        'delete' => ['post'],

                    ],

                ],

    ]);

}

You need to remember to add use yii\helpers\ArrayHelper at the top of the controller class.