{ Hello Magento 2 }

解决 Magento 2 应用问题,更注重深度挖掘。(ง •̀_•́)ง

0%

id and frontName in routes config file

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
2
3
<route id="mymodule" frontName="user-subscriptions">
<module name="MyCompany_MyModule"/>
</route>

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
2
3
<route id="mymodule" frontName="user-subscriptions">
<module name="MyCompany_MyModule" />
</route>

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
2
3
4
5
6
7
8
9
10
11
<?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="standard">
<route frontName="test_user" id="test">
<module name="VendorName_TestModule"/>
</route>
<route id="customer">
<module name="VendorName_TestModule" before="Magento_Customer" />
</route>
</router>
</config>
1
2
3
4
5
6
7
8
9
10
<?php
namespace VendorName\TestModule\Controller\Account;

class Login extends\Magento\Framework\App\Action\Action
{
public function execute()
{
echo 'customer login';
}
}

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