0

Мой компонент:

<?php
    namespace backend\components\example;
    use yii\base\Component;
    use yii\helpers\Html;

    class example extends Component {
        protected $token;
        public $options;
        public $id;
        public $secret;
        public $callback;
        protected $server = 'http://some.server.ru/';

        public function __construct($options, $config) {
            parent::__construct($config);
            var_dump($options);
        }

        public function example() {
        }

        public function init() {
            parent::init();
        }

        public function start() {
            echo $this->token;
        }
    }
?>

файл конфигурации:

<?php
'components' => [
    'i18n' => [
        'translations' => [
            'app*' => [
                'class' => 'yii\i18n\PhpMessageSource',
            ],
        ],
    ],
    'example' => [
        'class' => 'backend\components\example\example',
        'options' => [
            'id' => 'b1d249b3a53d47e6b7c4e2d334dbe601',
            'secret' => '5dc47ecbd5ae4fe9b1692957b2016b6e',
            'callback' => 'https://amgexpert.com',
        ],
    ],
];
?>

ошибка:

Invalid Configuration – yii\base\InvalidConfigException
Missing required parameter "options" when instantiating "backend\components\example\example"
  1. Как правильно передать/обработать настройки моего компонента? Пытался перехватить эту переменную options и в функции __construct и в init - везде вываливается ошибка.

1 ответ 1

1

Не нужно заворачивать его в options. Просто передавай параметры они отправятся уже в конструктор объекта:

'example' => [
    'class' => 'backend\components\example\example',
    'id' => 'b1d249b3a53d47e6b7c4e2d334dbe601',
    'secret' => '5dc47ecbd5ae4fe9b1692957b2016b6e',
    'callback' => 'https://amgexpert.com',
],

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.