2017-02-23 34 views
0

我在每個YII2創建API和每個請求運作良好,但在PUT要求我得到以下錯誤Yii2設置未知屬性:警予過濾器權威性 HttpBearerAuth ::格式

財產來源不明 - 警予\基地\ UnknownPropertyException

設置未知屬性:警予\過濾器\權威性\ HttpBearerAuth ::格式

這是我的堆棧跟蹤

  1. 在d:\ XAMPP \ htdocs中\連接\廠商\ yiisoft \ yii2 \基\ Object.php在線161 152153154155156157158159160161162163164165166167168169170
    */ 公共函數__set($名稱,$值) { $ setter ='set'。 $名稱;如果(method_exists($ this,$ setter)){ $ this - > $ setter($ value); ('this'($ this,'get'。$ name)){ throw new InvalidCallException('Setting the read-only property:'。get_class($ this)。'::'。$ name); ('設置未知屬性:'。get_class($ this)。'::'。$ name);}}返回新的UnknownPropertyException。 } }

    /**

    • 檢查一個屬性被設置,即定義和不爲空。 *
    • 不要直接調用此方法,因爲它是一種PHP魔術方法,當執行isset($object->property)時,將隱式調用
    • 。 *
  2. 在d:\ XAMPP \ htdocs中\連接\廠商\ yiisoft \ yii2 \ BaseYii.php在線路525 - YII \基\對象:: __集( '格式',[ '應用/ JSON' => 'json'])
  3. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Object.php在第105行 - yii \ BaseYii :: configure(yii \ filters \ auth \ HttpBearerAuth , ['only'=> ['index','view','create','update',...],'formats'=> ['application/json'=>'json']])
  4. yii \ base \ Object :: __ construct(['only'=> ['index','view','create','update',...],'formats'=> ['application/json '=>'json']])
  5. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ di \ Container.php在第381行 - ReflectionClass :: newInstanceArgs([['only'=> ['index', 'view','創建','更新',...],'格式'=> ['application/json'=> 'json']]])
  6. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ di \ Container.php 156行 - yii \ di \ Container :: build('yii \ filters \ auth \ HttpBearerAuth', [],['only'=> ['index','view','創建','更新',...],'格式' => ['application/json'=''json']])
  7. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2在第344行的BaseYii.php - yii \ di \ Container :: get('yii \ filters \ auth \ HttpBearerAuth',[], ['only'=> ['index' ,'查看','創建','更新',...],'formats'=> ['application/json'=''json']])
  8. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Component.php at 667 - yii \ BaseYii :: createObject(['class'=> 'yii \ filters \ auth \ HttpBearerAuth','only'=> ['index','view', 'create','update',... ],'formats'=> ['application/json'=> 'json']])
  9. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Component.php at line 651 - yii \ base \ Component :: attachBehaviorInternal(0,['class' =>'yii \ filters \ auth \ HttpBearerAuth','only'=> ['index','view','create','update', ...],'formats'=> ['application/json'=> 'json']])
  10. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Component.php在533行 - yii \ base \ Component :: ensureBehaviors()
  11. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Controller.php在第272行 - yii \ base \ Component :: trigger('beforeAction', yii \ base \ ActionEvent)
  12. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft yii \ base \ Controller :: beforeAction(yii \ rest \ UpdateAction)中的\ yii2 \ web \ Controller.php
  13. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Controller。 php在線154 - yii \ web \ Controller :: beforeAction(yii \ rest \ UpdateAction)
  14. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Module.php在第523行 - yii \ base \ Controller :: runAction('update',['id'=>'1'])
  15. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ web \ Application .php at line 102 - yii \ base \ Module :: runAction('products/update',['id'=> '1'])
  16. in D:\ xampp \ htdocs \ connect \ vendor \ yiisoft \ yii2 \ base \ Application.php at line 380 - yii \ web \ Application :: handleRequest(yii \ web \ Request)
  17. in D:\ xampp \ htdocs \ connect \ index.php at line 12 - yii \ base \ Application :: run()6789101112 require(DIR。 '/vendor/autoload.php');要求(DIR。 '/vendor/yiisoft/yii2/Yii.php'); $ config = require(DIR。 '/config/web.php'); (new yii \ web \ Application($ config)) - > run(); $ _GET = [ 'id'=>'1',]; Yii框架2017年2月23日,9時31分51秒

阿帕奇/ 2.4.23(Win32的)的OpenSSL/1.0.2h PHP/28年5月6日Yii框架/ 2.0.11.2

行爲在控制器

public function behaviors() { 
     return [ 
      [ 
       'class' => HttpBearerAuth::className(), 
       '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'] 
       ], 
      ] 
     ]; 
    } 

我與postman測試它在每個API傳遞Authorization

這是我的頭

enter image description here

可能是什麼問題?

回答

1

錯誤是明確的:HttpBearerAuth類沒有財產formats所以下面的行不應該是HttpBearerAuth配置下:

'formats' => ['application/json' => Response::FORMAT_JSON,], 

這是\yii\filters\ContentNegotiator屬性。有關更多詳細信息,請參見Content Negotiation上的文檔。

+0

我在標頭中傳遞'Content-Type:application/json'和'Accept:application/json'。我需要添加其他東西嗎? Doc正在說'$ curl -i -H「接受:application/json; q = 1.0,*/*; q = 0.1」「http:// localhost/users」' – urfusion

+0

我不認爲你需要添加任何東西其他。嘗試一下,看看 – topher

+0

還是一樣的錯誤。 – urfusion