Magento 2 routes.xml 用法解析
本文内容来自QQ群大佬陈伟明(一叶知秋)的学习分享
标准路由格式:
<store-url>/<store-code>/<front-name>/<controller-name>/<action-name>
- <store-url> - specifies the base URL for the Magento instance
- <store-code> - specifies the store context
- <front-name> - specifies the frontName of the FrontController to use (for example, [routesxml])
- <controller-name> - specifies the name of the controller
- <action-name> - specifies the action class to execute on the controller class
定义routes.xml
- /etc/routes.xml
- /etc/frontend/routes.xml
- /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="%routerId%"> <route id="%routeId%" frontName="%frontName%"> <module name="%moduleName%" before="Vendor_Module"/> </route> </router> </config>
%routerId%:
前端frontend有 robots urlrewrite standard cms default
后端adminhtml 有 admin default
%routeId%:路由标识
%frontName% 指明URL请求的名字
%moduleName% 在哪个模块
before 不是必须的,它是可以重写一个控制器
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <!--使用'standard'于前台路由--> <router id="standard"> <!--定义自定义路由的ID和frontName--> <route id="customer"> <module name="Magentochina_HelloWorld" before="Magento_Customer" /> </route> </router> </config>
若 HelloWorld 下面 Controller/Account/Login.php
这样就重写的 系统的客户Login 的方法