How do I get the host of the webshop in a backend controller? I thought Shopware()->Shop()->getHost() would do the trick and it does when I’m using a frontend controller, but when using this in a backend controller the shop returned from Shopware()->Shop() is always null. Can anyone tell me what causes this behaviour? Thnx in advance!
Hi,
the Backend does not have a Shop object, as it doesn’t “know”, which of your multishops to use. But you can create a ShopContext object easily:
/** @var \Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface $contextService */
$contextService = $this->get('shopware_storefront.context_service');
$host = $contextService->createShopContext(1)->getShop()->getHost();
You just need to replace the “1” with the shop ID you want to have.
Best regards,
Daniel
Hi Daniël,
Thank you for the quick response. I got it to work by using the following function in my controller to access the Shop object.
private function getShop() { $em = $this-\>get('models'); $repo = $em-\>getRepository(Shopware\Models\Shop\Shop::class); $shop = $repo-\>findById(1); return $shop[0]; }
Is there a significant downside to this method compared to the example you posted?
Best regards,
Donné