What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

}Hello, I am having trouble with dependency injection through the 'injector/' directory in the modu

U048WDEP3R7
U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet
edited January 2023 in Help

}Hello,

I am having trouble with dependency injection through the 'injector/' directory in the module. I saw in the documentation that you can inject dependency this way but it is not working for me. Can someone please guide me if this way works for you? I am trying to inject a dependency in CheckoutPageDepedencyProvider

Here is my code ...

config_default.php:

// ---------- Dependency injector
$config[KernelConstants::DEPENDENCY_INJECTOR_YVES] = [
    'CheckoutPage' => [
        PaymentMethodsConstants::PROVIDER_NAME,
    ],
];

MyProject\Yves\PaymentMethods\Dependency\Injector\PaymentDependencyProvider.php

<?php

namespace MyProject\Yves\PaymentMethods\Dependency\Injector;

use Spryker\Yves\DummyPayment\Dependency\Injector\CheckoutDependencyInjector;
use Spryker\Yves\Kernel\Container;
use MyProject\Yves\PaymentMethods\Plugin\DirectDebitSubFormPlugin;
use Spryker\Yves\Checkout\CheckoutDependencyProvider;
use Spryker\Yves\StepEngine\Dependency\Plugin\Form\SubFormPluginCollection;
use Spryker\Yves\Kernel\Dependency\Injector\DependencyInjectorInterface;


class CheckoutPageDependencyInjector implements DependencyInjectorInterface
{
    /**
     * @param \Spryker\Yves\Kernel\Container $container
     *
     * @return \Spryker\Yves\Kernel\Container
     */
    protected function injectPaymentSubForms(Container $container): Container
    {
        $container->extend(
            CheckoutDependencyProvider::PAYMENT_SUB_FORMS,
            function (SubFormPluginCollection $paymentSubForms) {
               $paymentSubForms->add(new DirectDebitSubFormPlugin());

            return $paymentSubForms;
        });


        return $container;
    }

    public function inject(Container $container): Container
    {
        $container = $this->injectPaymentSubForms($container);

        return $container;
    }
}

Comments

  • U019WGU7SM9
    U019WGU7SM9 Posts: 24 🧑🏻‍🚀 - Cadet

    Hi, what is the value of PaymentMethodsConstants::PROVIDER_NAME? I believe it should be PaymentMethods

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet

    @U019WGU7SM9 Yes it is PaymentMethods

  • U019WGU7SM9
    U019WGU7SM9 Posts: 24 🧑🏻‍🚀 - Cadet
    edited January 2023

    Well that looks ok to me then - a couple of more things you could check
    1. clear your resolver cache: vendor/bin/console cache:class-resolver:build
    2. I’m not sure whether you have to extend the AbstractDependencyInjector, but might be worth trying
    3. I assume you’ve already been working with code in the MyProject namespace, but if you haven’t, then it’ll need to be added to $config[KernelConstants::PROJECT_NAMESPACES]

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet

    @U019WGU7SM9 Thanks. The namespace is added in the project namespaces. All other files work just not this one. I am going to try your 2nd point now.

  • sebastian.larisch
    sebastian.larisch Spryker Customer Posts: 143 🧑🏻‍🚀 - Cadet

    I miss use Spryker\Yves\Kernel\Dependency\Injector\DependencyInjectorInterface; in your injector class

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet

    @UNGMX0012 forgot to add it in the message, I do have it in the code. Updated.

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet

    @UNGMX0012 @U019WGU7SM9 looks like the config settings in config_oms-development.php are overiding the settings in config_default.php

  • U019WGU7SM9
    U019WGU7SM9 Posts: 24 🧑🏻‍🚀 - Cadet

    aha