Setting up an Admin Route in Magento 2

The difference is in urls. Url has following structure: <areaFrontName>/<moduleFrontName>/<actionPath>/<actionName>

Route "Adminhtml" has moduleFrontName "admin", same as areaFrontName. So all paths under "adminhtml" route will start with admin/admin.

If you want to have a more specific url, you should use specific route, like catalog does it. Catalog urls all start with admin/catalog. This is the preferred way.

So preferred configuration is:

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

Note that before="Magento_Backend" is not needed


I came across this as well while looking for examples how to add an adminhtml controller. I did some research and this is what I've found.

The route id="adminhtml" way is used 24 times in the core.

The before="Magento_Backend" way is used 31 times in the core.

There are only 50 modules with an adminhtml/routes.xml but 24+31 = 55. Hint #1.

I tried to find a common denominator between what type of modules uses which type, but I can't seem to identify any. So maybe the change was introduced at a certain point in time so I checked the timing on the two types. Unfortunately this was quite hard since most of them were last edited mid-september to introduce the new urn's so I had to use Github's history function.

I then noticed some routes.xml files where the route id="adminhtml" option is used also use the before="Magento_Backend", see for example Magento_UrlRewrite's routes.xml file. I can't seem to find any conclusive commonalities between the three variants.

I also checked the new modules (which weren't in M1 and therefore could not be ported but were written new for M2), such as AdvancedPricingImportExport, Integration, MediaStorage and EncryptionKey and although some use the before="Magento_Backend" and some don't, they all use the <route id="adminhtml"> tag. Amongst these, the ones without the before="Magento_Backend" were last changed in February 2015 while the ones with that tag all were edited after that date.

So, my preliminary conclusion is that this is the preferred way to do it (either explicitly decided on internally at Magento HQ or not);

<config xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <router id="admin">
        <route id="adminhtml">
            <module name="Your_Extension" before="Magento_Backend"/>
        </route>
    </router>
</config>

I'd love to hear Magento core dev's position on this, obviously.

UPDATE: Anton Kril responded, see his answer for the preferred way to do this.