3

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

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]);
  • You can do like this also. $this->_storeManager->getStore()->getBaseUrl(), $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface') ->getStore($storeId) ->getBaseUrl(); – Ravi Thanki Aug 1 '16 at 5:41
  • yes but it forbidden. You should not get set data via objectmanager – mrtuvn Aug 1 '16 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 '16 at 9:26
3

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);
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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