Handle multiple channels in CLI =============================== When we use directly or indirectly any service depending on a channel context, we are not able to define which channel we want to use, when there are more than two. Your primary goal should be to avoid such cases, but if you have to, there is a way to do it. What is our goal? ----------------- We have to create a custom channel context available only from the CLI context. This channel context should allow setting a channel code (which we will do inside the console command). This way, we can use the channel context in our services. 1. Custom Channel Context ------------------------- First, we need to create a channel context. Let's remind the requirements: * available only from the CLI * there must be a way to pass in a channel code Our suggested solution is the following: .. code-block:: php // src/Channel/Context/CliBasedChannelContext.php channelCode = $channelCode; } public function getChannelCode(): ?string { return $this->channelCode; } public function getChannel(): ChannelInterface { if ('cli' !== PHP_SAPI || null === $this->channelCode) { throw new ChannelNotFoundException(); } $channel = $this->channelRepository->findOneByCode($this->channelCode); if (null === $channel) { throw new ChannelNotFoundException(); } return $channel; } } .. code-block:: php // src/Channel/Context/CliBasedChannelContextInterface.php; addOption('channel', 'c', InputOption::VALUE_OPTIONAL, 'Channel code') ; } protected function execute(InputInterface $input, OutputInterface $output): int { if (null !== $channelCode = $input->getOption('channel')) { $this->cliBasedChannelContext->setChannelCode($channelCode); } // The subscriber just gets a channel from the channel context $this->dispatcher->dispatch(new DummyEvent()); return Command::SUCCESS; } } The output of the example is following: .. code-block:: bash $ bin/console app:dummy -c MAGIC_WEB Hi! I am Dummy Event Subscriber. I am using Channel Context. Your channel name is: Magic Web Channel $ bin/console app:dummy -c FASHION_WEB Hi! I am Dummy Event Subscriber. I am using Channel Context. Your channel name is: Fashion Web Store