Magento 2 – Can Multiple Modules Share a FrontName in routes.xml?

adminhtmlmagento2routing

In my current project I have developed several modules to add different pieces of functionality. I'd like the admin of each module to be managed from a shared frontName, for example:

  • Company_ProductModule (/admin/company/product/index…)
  • Company_CategoryModule (/admin/company/category/index…)
  • Company_CustomerModule (/admin/company/customer/index…)

I have another module, Company_AdminModule, which has this adminhtml/routes.xml:

<router id="admin">
    <route id="company" frontName="company">
        <module name="Company_AdminModule">
    </route>
</router>

The admin module has an adminhtml/menu.xml as follows:

<menu>
    <add id="Company_Admin::main" title="Company Name" module="Company_Admin" />
</menu>

Each module has an adminhtml/menu.xml as follows:

<menu>
    <add id="Company_Product::index" title="Product Management" module="Company_Product" parent="Company_Admin::main" action="company/product/" />
</menu>

This works in so far as the menu items are all added, and the URLs all look correct. However visiting each link added by a module which isn't Company_AdminModule just redirects to the admin homepage, I guess because it's looking for the controller in the Company_AdminModule module.

Is there any way to split out the controllers under this frontName, so each can sit within the correct module's Controller/Adminhtml/ directory?

Ultimately this is desirable because each additional module should be able to add to the admin menu (which it can) but also add to the URL structure in a 'neat' way.

Best Answer

I've honestly never tried, but best I can make out looking at examples from core, you should just need to add:

<router id="admin">
    <route id="company">
        <module name="Company_OtherModule">
    </route>
</router>

To each of your other extensions in order to register the modules controllers for that route.

Related Topic