0
votes

I am creating apis in YII2 each and every request is working well but in PUT request I am getting below error

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\filters\auth\HttpBearerAuth::formats

This is my stack trace

  1. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Object.php at line 161 152153154155156157158159160161162163164165166167168169170
    */ public function __set($name, $value) { $setter = 'set' . $name; if (method_exists($this, $setter)) { $this->$setter($value); } elseif (method_exists($this, 'get' . $name)) { throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); } else { throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); } }

    /**

    • Checks if a property is set, i.e. defined and not null. *
    • Do not call this method directly as it is a PHP magic method that
    • will be implicitly called when executing isset($object->property). *
  2. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\BaseYii.php at line 525 – yii\base\Object::__set('formats', ['application/json' => 'json'])
  3. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Object.php at line 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 at line 381 – ReflectionClass::newInstanceArgs([['only' => ['index', 'view', 'create', 'update', ...], 'formats' => ['application/json' => 'json']]])
  6. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\di\Container.php at line 156 – yii\di\Container::build('yii\filters\auth\HttpBearerAuth', [], ['only' => ['index', 'view', 'create', 'update', ...], 'formats' => ['application/json' => 'json']])
  7. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\BaseYii.php at line 344 – yii\di\Container::get('yii\filters\auth\HttpBearerAuth', [], ['only' => ['index', 'view', 'create', 'update', ...], 'formats' => ['application/json' => 'json']])
  8. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Component.php at line 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 at line 533 – yii\base\Component::ensureBehaviors()
  11. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Controller.php at line 272 – yii\base\Component::trigger('beforeAction', yii\base\ActionEvent)
  12. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\web\Controller.php at line 164 – yii\base\Controller::beforeAction(yii\rest\UpdateAction)
  13. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Controller.php at line 154 – yii\web\Controller::beforeAction(yii\rest\UpdateAction)
  14. in D:\xampp\htdocs\connect\vendor\yiisoft\yii2\base\Module.php at line 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'); require(DIR . '/vendor/yiisoft/yii2/Yii.php'); $config = require(DIR . '/config/web.php'); (new yii\web\Application($config))->run(); $_GET = [ 'id' => '1', ]; Yii Framework 2017-02-23, 09:31:51

Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28 Yii Framework/2.0.11.2

Behaviors in controller is

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

I am testing it with postman and passing Authorization in each api.

This is my header

enter image description here

What could be the issue?

1

1 Answers

1
votes

The error is clear: the HttpBearerAuth class has no property formats so the following line shouldn't be under the HttpBearerAuth config:

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

This is a property of \yii\filters\ContentNegotiator. See the docs on Content Negotiation for more details