Notes
The standard router URL structure is {frontName}/{actionPath}/{action}
(standard router class: magento\framework\App\Router\Base.php::match) So, the catalog/product_compare/add
correspond to vendor\magento\module-catalog\Controller\Product\Compare\Add.php
The naming convention for layout.xmls is {route id}_{controller name}_{action name}.xml
Practice tests
You see this code in etc/frontend/routes.xml:
1 | <route id="mymodule" frontName="user-subscriptions"> |
You have placed a controller in Controller/Index/Subscribe.php. At what URL would you visit the controller listed above? A. /mymodule/index/subscribe B. /mymodule/subscribe C. /user-subscriptions/subscribe D. /user-subscriptions/index/subscribe Answer D
You see this code in etc/adminhtml/routes.xml:
1 | <route id="mymodule" frontName="user-subscriptions"> |
You have placed a controller in Controller/Index/Subscribe.php. If you want to create layout XML instructions for this controller, what would be the layout XML’s filename? A. user_subscriptions_subscribe_[ACTION NAME].xml B. user_subscriptions_index_subscribe.xml C. mymodule_index_subscribe.xml D. mymodule_subscribe_[ACTION NAME].xml Answer C
Experiments
Today we are going to use route to rewrite controller. We’ve created a simple module VendorName_TestModule
. etc/frontend/routes.xml
1 | <?xml version="1.0" ?> |
1 | <?php |
Ok, after we visit http://example.com/index.php/customer/account/login
, we will go into our controller.
Reference
Why to keep id and frontName same in magento 2 routes config file? Magento 2 Routing