In admin code, i would like to get the frontend store url for a path. But, always getting admin url for the path.
$storeId = $this->_storeManager->getDefaultStoreView()->getStoreId();
$url = $this->_storeManager->getStore($storeId)->getUrl("sample/index/test");
Expected result in http://{magento-url}/sample/index/test/, but always getting http://{magento-url}/{admin-path}/sample/index/test/
Checked the store model, the getUrl function uses Magento\Backend\Model\Url to get the store url.
Can anybody please, let me know the correct way to get the store url from admin page.
namespace VendorName\ModuleName\Model;
use VendorName\ModuleName\Helper\Data as ModuleHelper;
use Magento\Store\Model\StoreManagerInterface;
class Api {
protected $_helper;
public function __construct(ModuleHelper $helper, StoreManagerInterface $storeManager) {
$this->_helper = $helper;
$this->_storeManager = $storeManager;
}
public function doSomeAction() {
//$customUrl = $this->_storeManager->getStore()->getBaseUrl() . "/sample/index/test";
$customUrl = $this->_helper->getCustomUrl();
echo $customUrl;
}
}
namespace VendorName\ModuleName\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper {
protected $_storeManager;
public function __construct(Context $context, StoreManagerInterface $storeManager) {
parent::__construct($context);
$this->_storeManager = $storeManager;
}
public function getCustomUrl() {
$storeId = $this->_storeManager->getDefaultStoreView()->getStoreId();
return $this->_storeManager->getStore($storeId)->getUrl("sample/index/test");
}
}