15

Looking at the core Magento 2 code, there seems to be two ways of setting up an admin route.

The first

#File: vendor/magento/module-media-storage/etc/adminhtml/routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="adminhtml">
            <module name="Magento_MediaStorage" />
        </route>
    </router>
</config>

is similar to Magento 1. You add your module to a list of modules in the adminhtml route, and Magento checks it for you.

The second

#File: vendor/magento/module-cms/etc/adminhtml/routes.xml
<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" before="Magento_Backend" />
        </route>
    </router>
</config>

has you setting up a new route (id cms above), and then adding your module to it.

Is one of these the "right/preferred" way of doing this in Magento 2? If not, what are the difference between the two? i.e. when would you use one over the other?

Asking not to solve a specific problem, but to make sure I'm creating my routes in the correct way and that problems similar to Magento 1 modules (pulling in ajax libraries, security, etc.) are avoided.

22

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

  • 3
    are there any explanations on naming ID and frontName? should it be the same for admin area? should it use module name? why it is not working when id != frontName ? – Sergey Korzhov Jul 28 '16 at 16:11
4

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.

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.