8

I am developing a extension of Magento 2. I need to get a product's frontend url from backend. I have tried to use:

//$storeManager->->setCurrentStore(2);
$url = $product->setStoreId(2)->getProductUrl();
$url = $product->setStoreId(2)->getUrlInStore(); 

but the $url always return a backend url, like http://<domain>/admin/catalog/product..... they may work in Magento 1.x but not for 2.x. Is there any way to get frontend url?

1

3 Answers 3

7

Add new constructor dependency to your class forntUrlModel:

function __construct(
    ...
     \Magento\Framework\UrlInterface $frontUrlModel
) {
    $this->frontUrlModel = $frontUrlModel;
}

private function getProductUrl($product, $storeCode = 'default', $categoryId = null) {
        $routeParams = [ '_nosid' => true, '_query' => ['___store' => $storeCode]];

        $routeParams['id'] = $product->getId();
        $routeParams['s'] = $product->getUrlKey();
        if ($categoryId) {
            $routeParams['category'] = $categoryId;
        }
     return $this->frontUrlModel->getUrl('catalog/category/view', $routeParams);
 }

And use DI to inject right dependency

<type name="YouClass"> 
    <arguments>
        <argument name="frontUrlModel" xsi:type="object" shared="false">Magento\Framework\Url</argument>
    </arguments>
</type>
4
  • thanks. I see you inject UrlInterface and define dependency in Di.xml. Why don't you inject \Magento\Framwork\Url directly?
    – Paul Dong
    Nov 1, 2016 at 22:09
  • Interface segregation principle, from S.O.L.I.D
    – KAndy
    Nov 2, 2016 at 9:23
  • @KAndy i had tried this.. but unable to get the product url.. :( please post the full code here.. I'm trying to make a column in product grid with product frontend link.
    – Ask Xah
    Oct 21, 2018 at 8:38
  • @KAndy what is the YouClass means? we need to make any class here. Can you please more elaborate it so this answer can help many others like me Dec 11, 2018 at 4:25
7

I answer my question. mtns_cll's answer the question of Magento 2 get frontend store url for a path in admin works for me.

I post my solution here in case someone need it:

product frontend:

inject \Magento\Framework\Url $url

$url->getUrl('catalog/product/view', ['id' => $entityId, '_nosid' => true, '_query' => ['___store' => $storeCode]]);

product backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl('catalog/product/edit', ['id' => $entityId, 'store' => $targetStoreId]);

category frontend

inject `\Magento\Framework\Url $url

$url->getUrl('catalog/category/view', ['id' => $entityId, '_nosid' => true, '_query' => ['___store' => $storeCode]]);

category backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl('catalog/category/edit', ['id' => $entityId, 'store' => $targetStoreId]);

cms page frontend

inject Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder $rul

$url->getUrl($this->_pageModel->getIdentifier(), $targetStoreId, $storeCode );

cms page backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl(PageActions::CMS_URL_PATH_EDIT, ['page_id' => $pageId]);

cms block backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl(BlockActions::URL_PATH_EDIT, ['block_id' => $blockId]);
1

You can try to use Magento/Store/Model/StoreManager. Inject it into a constructor via Magento/Store/Model/StoreManagerInterface, more or less like this:

    public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
   .....
) {
   ...
$this->_storeManager=$storeManager;
}
$requestedStoreObject = $this->_storeManager->getStore($storeId);
$urlToRequestedStore = $requestedStoreObject->getUrl();

You should notice that function getStore() as an argument can take not only integer, but also string or object:

/**
 * Retrieve application store object
 *
 * @param null|string|bool|int|\Magento\Store\Api\Data\StoreInterface $storeId
 * @return \Magento\Store\Api\Data\StoreInterface
 */
public function getStore($storeId = null);
1
  • 1
    It doesn't work for me. it returns backend url, like http://<domain>/admin/....
    – Paul Dong
    Nov 1, 2016 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.