4

can anyone explain what is the right situation to use before and after attribute in route.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="admin">
        <route id="routename" frontName="routename">
            <module name="vendor_module" before="Magento_Backend" />
        </route>
    </router>
</config>

I am looking well explain answer.

5

You need to use a before or after tag to control the order, Magento will check for matches.

Using the after tag above ensures Magento checks our module after the Magento_Backend module. Without this, it would be possible for us to accidentally create a controller file that replaced the core controllers in vendor/magento/module-backend/Controller and before tag is as same as reverse to after.

For more information you can also refer link http://alanstorm.com/magento_2_advanced_routing

5

Magento provide the attribute before/after to config the module sort order which define what module controller will be find first. This is the logic for the controller rewrite.

For example, if we want to rewrite the controller customer/account/login, we will define more route in the routes.xml like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
   <!--Use router 'standard' for frontend route-->
   <router id="standard">
       <route id="customer">
           <module name="Vendor_Module" before="Magento_Customer" />
       </route>
   </router>
</config>

Add the controller file: app/code/Vendor/Module/Controller/Account/Login.php

So the frontController will find the Login action in our module first, if it’s found, it will run, and the Login action of Magento_Customer will not be run. Then we are successful rewrite a controller.

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.