M
M
Mark2021-10-11 21:34:56
Yii
Mark, 2021-10-11 21:34:56

Why doesn't the dependency container see the parameter?

Exception 'yii\base\InvalidConfigException' with message 'Missing required parameter "location" when instantiating "shop\manage\payment\cloudPayments\CloudPaymentsPaymentCreator".'


The parameter is passed to the get()

\Yii::$container->get($class, [], $params);

Printout method $params:

array(1) {
  ["location"]=>
  string(2) "EU"
}


Class constructor CloudPaymentsPaymentCreator:

class CloudPaymentsPaymentCreator implements PaymentCreator
{
    const LOCATION_USA = "USA";
    const LOCATION_EU = "EU";

    private $location;

    public function __construct($location)
    {
        $this->location = $location;
    }


Full function code using container

public static function create($method)
    {
        foreach (self::$map as $receiptMethod => $classParams) {
            if ($method === $receiptMethod) {
                /**
                 * @var $class
                 * @var $params
                 */
                extract(self::prepareParams($classParams));

                echo "\n\n";
                var_dump($params);
                echo "\n";

                return \Yii::$container->get($class, [], $params);
            }
        }

        throw new \DomainException("Unsupported payment system");
    }

    protected static function prepareParams($classParams)
    {
        return is_array($classParams) ? $classParams : ['class' => $classParams, 'params' => []];
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question