2016-05-04 10 views
5

nel mio controllerYii2 API REST metodo PUT non funziona

` namespace app \ api \ moduli \ v1 \ controllori;

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

     ] 
    ]; 
} 
}` 

cerco dal mio POSTINO App

Per creo io uso POST http://localhost/myapp/api/v1/countries Opere fine.But Per Aggiornamento io l'impiego, mettere http://localhost/myapp/api/v1/countries/16 restituisce 16 record come uscita JSON non aggiornare come previsto.

Cosa c'era di sbagliato? Grazie!!

+0

Se la chiamata PUT restituisce l'oggetto corretto sembra che l'updateAction sta lavorando bene. Sei sicuro che i valori che stai postando siano nel set di 'rules()'? – jagsler

risposta

0

Questa è un'altra opzione se ti senti a tuo agio nell'utilizzarlo. Invece di behaviors() puoi aggiungere qualcosa come questo e avrà lo stesso scopo e non avrai alcun problema.

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

In POSTINO App, aprire la scheda Richiesta corpo e selezionare x-www-form-urlencoded invece di form-data. Questo ha funzionato per me.

x-www-form-urlencoded selected