2017-02-23 154 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