Magento 2 get url for specific store
Pregunta
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.
Solución 2
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);
Otros consejos
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 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange