9

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");
    }

}
5
  • see my updated answer
    – ND17
    Aug 23, 2016 at 10:29
  • I am currently using getBaseUrl() . "path". But, i think this is not the correct way. So, wanted a correct way to get the url path. Aug 23, 2016 at 10:32
  • I found this app\code\Magento\Backend\view\adminhtml\templates\page\header.phtml ,In this it is using for customer view
    – ND17
    Aug 23, 2016 at 10:36
  • You can check cms page grid action, how they create url for preview
    – ND17
    Aug 23, 2016 at 10:50
  • can you answer me this question magento.stackexchange.com/questions/132369/… Aug 23, 2016 at 12:22

4 Answers 4

18

Might be too late for OP, but hopefully this helps anyone else that lands on this page with the same issue.

The reason you are not getting the frontend URL is due to a preference defined in the core code, specifically:

magento/module-backend/etc/adminhtml/di.xml (line 12 in CEv2.1)

<preference for="Magento\Framework\UrlInterface" type="Magento\Backend\Model\UrlInterface" />

In order to solve it you have two options

1) Create your own preference for UrlInterface (in your own module), and determine in your code when the backend/frontend URL should be returned. (strictly speaking, this would be most in-line with Magento code guidelines, but IMO is overkill if you only need it a few times)

2) Call the frontend Url.php (which implements UrlInterface) in the constructor of the file you need to get the URL in. This will bypass the preference and give you a frontend URL. Partial code example shown below

/** @var \Magento\Framework\Url */
protected $urlHelper;

public function __construct(
    <<insert your other required files here>>
    \Magento\Framework\Url $urlHelper,
) {
    <<insert parent/other required objects here>>
    $this->urlHelper = $urlHelper;
}

public function getFrontendUrl($routePath, $routeParams)
{
    return $this->urlHelper->getUrl($routePath, $routeParams)
}

public function yourFunction()
{
    $url = $this->getFrontendUrl(
        'quotes/history/view',
        array(  //any parameters should be passed in array format
            'id' => $mageQuote->getId(), 
            '_nosid' => true  //prevents sessionId from getting added
        )
    );
    // $url = 'https://www.domainname.com/quotes/history/view/id/###/'
}
4
  • 5
    It took my a while to work out how to specify a particular store to generate it from (important if you have multiple stores). I passed the store_id to the options array as follows: $this->urlHelper->getUrl( 'quotes/history/view', [ '_scope' => $storeId, 'id' => $mageQuote->getId(), '_nosid' => true ]); Sep 13, 2017 at 13:37
  • @RobertEgginton thanks, it worked perfectly for me Mar 30, 2018 at 5:33
  • All possible URL parameters can be found here: \Magento\Framework\Url::$_reservedRouteParams Jun 25, 2018 at 21:14
  • @RobertEgginton its not working for me in Magento 2.3.2 Mar 19, 2020 at 12:54
4

You can get store url from admin by passing the scope parameters as following

/** @var \Magento\Framework\Url */
protected $urlHelper;

public function __construct(
    \Magento\Framework\Url $urlHelper
) {
    $this->urlHelper = $urlHelper;
}

public function getFrontendUrl()
{
    $storeId = 1;
    $id = 10;
    return $this->urlHelper->getUrl( 'quotes/history/view', [ '_scope' => $storeId, 'id' => $id, '_nosid' => true ]);
}
1
0

You can write code in Helper/Data.php to get store url and using data helper call that method in admin page

for example

Helper/Data.php

public function getStoreUrl(){
    $storeId = $this->_storeManager->getDefaultStoreView()->getStoreId();
    $url = $this->_storeManager->getStore($storeId)->getUrl("sample/index/test");
return $url;

}

In admin page In block

public function __construct(
    \Namespace\Modulename\Helper\Data $_dataHelper
) {
    $this->_dataHelper = $_dataHelper;
}

get url - $this->_dataHelper->getStoreUrl();

In phtml

$block->helper('Namespace\Modulename\Helper\Data')->getStoreUrl();

For Magento 2.1

in block or phtml

echo $this->getBaseUrl().'sample/test/test'; 
7
  • 1
    Sorry, i am still getting the admin url only.... http://{magento-url}/{admin-path}/sample/index/test/ Aug 23, 2016 at 6:16
  • @santhoshkumar can you show me how you implemented this code?
    – ND17
    Aug 23, 2016 at 8:11
  • Added the sample code in the question Aug 23, 2016 at 9:11
  • @santhoshkumar i tested this code its working its giving frontend url
    – ND17
    Aug 23, 2016 at 9:44
  • you tested in your frontend page or backend page? call the url in the admin page. I am using magento 2.1.0. Aug 23, 2016 at 9:47
0

Just use websiteRepositoryInterface like this:

use Magento\Store\Api\WebsiteRepositoryInterface

Now declare it in class constructor and then use it like:

$this->websiteRepositoryInterface->getDefault()->getDefaultStore()->getUrl("xyz/test/index")

It will give you default website front-end url.

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.