Magento – Magento 2: get Logo URL for a specific store in controller

magento2store-view

I just started developing a module in Magento 2. It is a copy of a module I made in Magento 1.

I'm already stuck with Magento 2 logic.

I want to get the logo URL of every store view.

In Magento 1 I went like this :

Mage::getDesign()->getSkinUrl(Mage::getStoreConfig('design/header/logo_src', $storeId));

with $storeId being a specific store view ID.

I can't find a way to do it in Magento 2. Actually I have this.

$om = \Magento\Framework\App\ObjectManager::getInstance(); 
$logo=$om->get('Magento\Theme\Block\Html\Header\Logo');
$logoUrl=$logo->getLogoSrc();

But it only gives me one Url and I don't see how to inject the store Id in the process to have the other.

I guess that I have to change some Global Value which define the store but I can't find How.

I have found similar questions but answers don't apply to my case because I'm in a Controller so it's only back end.

If someone got an idea. I would be really thankful.

Best Answer

First of all using ObjectManager is not a recommended way. You should read a bit more into using Service Contract. Right now the core Theme module doesn't have service contract for what we want to do here, so you should inject the LogoFactory in.

Now to answer your question:

Inject \Magento\Framework\App\Config\ScopeConfigInterface into your controller, then use it to get the logo path, sample fore core code Magento\Theme\Block\Html\Header\Logo:_getLogoUrl :

$folderName = \Magento\Config\Model\Config\Backend\Image\Logo::UPLOAD_DIR;
        $storeLogoPath = $this->_scopeConfig->getValue(
            'design/header/logo_src',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        $path = $folderName . '/' . $storeLogoPath;
        $logoUrl = $this->_urlBuilder
                ->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]) . $path;