4

I need to render inside admin grid preview url for specific store (depends on which store is selected). In Magento 1 the code is this:

$storeId = (int)$this->getRequest()->getParam('store');
if ($storeId == 0) {
    $stores = Mage::app()->getStores(false, true);
    $storeId = current($stores)->getId();
}

$storeCode = Mage::app()->getStore($storeId)->getCode();        
$urlModel = Mage::getModel('core/url')->setStore($storeId);       
$url = $urlModel->getUrl(
    'banner/index/preview/id/'.$row->getId(), array(
        '_current' => false,
        '_query' => '___store='.$storeCode
   )
);

I ended up with this:

$storeId = (int)$this->request()->getParam('store');
if ($storeId == 0) {
    $stores = $storeManager->getStores(false);
    $storeId = current($stores)->getId();
}

$storeCode = $storeManager->getStore($storeId)->getCode();        

But I don't find any code snippet for Magento 2 to migrate $urlModel part of code from Magento 1.

Tnx for any idea.

3 Answers 3

5

I managed to get url with this method:

  \Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder $actionUrlBuilder

  $this->actionUrlBuilder->getUrl('route_path', $storeId, $code);
4

inject storeManagerInterface in constructor

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

Now you can GetUrl by command

$this->_storeManager->getStore()->getUrl('routepath', ['_current' => true]);
3
  • You can do like this also. $this->_storeManager->getStore()->getBaseUrl(), $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface') ->getStore($storeId) ->getBaseUrl(); Aug 1, 2016 at 5:41
  • yes but it forbidden. You should not get set data via objectmanager
    – mrtuvn
    Aug 1, 2016 at 7:17
  • 1
    Unfortunately this still shows url from admin: $this->_storeManager->getStore(2)->getUrl('routepath', ['_current' => true]); shows http://dev.magento2.com/admin/routepath/index/index/key/9f0007091ce7cdd44b20f4020004bff6fa4157f25b4034c18efc71f328f861fb/
    – JohnyFree
    Sep 7, 2016 at 9:26
0

You can get url for specific store using an instance of StoreManagerInterface it should be

$this->_storeManager->getStore($storeId)->getBaseUrl();

Other types of url can be found here: http://www.metagento.com/blog/base-url-in-magento-2.html

1
  • 1
    The question was not about base URLs. Feb 26, 2017 at 20:19

Your Answer

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

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