Inject dependencies within factories: Container globals

Edit on GitHub

The ContainerInterface provides a way to make dependencies globally available. Every dependency added to ContainerInterface, that is, marked as isGlobal, is available by using getProvidedDependency() in your factory.

To add something globally you need to add the dependency to the ContainerInterface and mark it as global.


namespace ProjectName\Application\Bundle\ApplicationPlugin;

use \Spryker\Shared\ApplicationExtension\Dependency\Plugin\ApplicationPluginInterface;

class YourApplicationPlugin implements ApplicationPluginInterface
     * {@inheritDoc}
     * - Adds global service.
     * @api
     * @param \Spryker\Service\Container\ContainerInterface $container
     * @return \Spryker\Service\Container\ContainerInterface
    public function provide(ContainerInterface $container): ContainerInterface
        $container->set('Your service name', function () {
            return new YourService();

        $container->configure('Your service name', ['isGlobal' => true]);

To access this global dependency, inside your factory, call $this->getProvidedDependency('Your service name'). With this approach, you can define such dependencies once instead of defining them each time for each module that uses them.