1

Just curious about Why to keep id and frontName same in magento 2 routes config file ?

Below is Sample frontend route xml config file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="mastering" frontName="mastering">
            <module name="Mastering_SampleModule" />
        </route>
    </router>
</config>

Thanks

1
  • 1
    its not mandatory to keep both value same Dec 28, 2017 at 11:34

1 Answer 1

5

The route element id and frontName attributes have the same value, but they do not serve the same purpose.

app/code/Mage/Example/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Mage_Example" />
        </route>
    </router>
</config>

Route definition requires a unique id and frontName attribute values, which in our case both equal helloworld.

The frontName attribute value becomes the part of our URL structure. Simply put, the URL formula for hitting the Crud action goes like:

{magento-base-url}/index.php/{route frontName}/{controller name}/{action name}

For example, if our base URL were http://domain.com/, the full URL would be http://domain.com/index.php/helloworld/test/crud/.

Note: If we have URL rewrites turned on, we could omit the index.php part.

namespace Mage\Example\Controller;
abstract class Test extends \Magento\Framework\App\Action\Action
{

}

namespace Mage\Example\Controller\Test;
class Crud extends \Mage\Example\Controller\Test
{
    public function __construct(
        /* Code Here */
    )
    {
        /* Code Here */
        return parent::__construct($context);
    }

    public function execute()
    {
        /* CRUD Code Here */
    }
}
4
  • 1
    vah pratik fine explanation Dec 29, 2017 at 5:39
  • Happy to help. Keep helping :)
    – Pratik Oza
    Dec 29, 2017 at 5:48
  • I want to achieve this url myproject.com/style/parameter/the-value .is this posible? Sep 4, 2018 at 14:01
  • 3
    One important thing to note here is that the naming convention for layout.xmls is {route id}_{controller name}_{action name}.xml. You have to keep this in mind, if your routeId is diferent from your frontName.
    – fritzmg
    Sep 14, 2018 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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