1

I'm currently working with URL rewrites and I'm looking for a way to get the front name of a route from a module.

<router id="standard">
    <route id="support" frontName="faq">
        <module name="" />
    </route>
</router>

This is for a module that I'm customizing, not a new module. I don't want to have to change the id as I would probably need to change quite a bit of code.

I'd like to get the 'faq' front name by supplying the id 'support' (which it does for getUrl(), but that returns the front name appended to the base URL.

If all else fails I could probably just use the getUrl() method and just grab the last fragment I guess, but I'd like to use a better solution if possible.

1

Please use this way

 getUrl("support/index/index");
0

routes.xml

When set the url by routes which id and frontName must be same ( ie, id="support" and frontName="support ")

The routes.xml file maps which module to use for a URL with a specific frontName and area. The location of the routes.xml file in a module, either etc/frontend or etc/adminhtml, specifies where those routes are active.

The content of this file uses the following format:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="%routerId%">
        <route id="custom" frontName="custom">
            <module name="%moduleName%"/>
        </route>
    </router>
</config>

For more details refer the link https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html

0

I ended up creating this function in the module's helper class and pulling the helper into whatever class I needed it via di (in the __construct method).

/**
 * Get the frontend url for the given path
 *
 * @param string $route
 * @param string $type base,path,frontname
 */
public function getFrontUrl($route = '', $type = 'base')
{
    if (empty($route)) {
        return false;
    }
    $storeId = $this->_storeManager->getDefaultStoreView()->getStoreId();
    $url = $this->_storeManager->getStore($storeId)->getUrl($route);

    if ($type == 'path' || $type == 'frontname') {
        $parsed = ltrim(parse_url($url, PHP_URL_PATH), '/');
        if ($type == 'path') {
            return $parsed;
        } else {
            $parsedFront = explode('/', $parsed);
            return $parsedFront[0];
        }
    } else {
        return $url;
    }
}

Which basically lets me get the frontend url or some variants of it:

$_helper->getFrontUrl('support')
    // returns 'http://example.com/faq/

$_helper->getFrontUrl('support', 'path')
    // returns 'faq/'

$_helper->getFrontUrl('support/view', 'path')
    // returns 'faq/view'

$_helper->getFrontUrl('support', 'frontname')
    // returns 'faq', even if the route specified was 'support/view'

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.