Routing 为 Magento 2 一个重要的部分,本文介绍基本应用
Routing 为 Magento 2 一个重要的部分,本文介绍基本应用
Magento 2请求的流程
在Magento 2中,请求URL将如下所示:
在该URL中,您将看到将用于查找模块的front_name。路由器通过在routes.xml中定义为每个模块定义此名称,我们将在下面看到更多细节。
当您在Magento 2中发出请求时,它将按照此流程查找 controller/action
: index.php → HTTP app → FrontController → Routing → Controller processing → etc
该FrontController
会在HTTP中类中调用路由信息,会发现该请求controller/action
匹配。
文件: vendor/magento/framework/App/FrontController.php
public function dispatch(RequestInterface $request)
{
\Magento\Framework\Profiler::start('routers_match');
$routingCycleCounter = 0;
$result = null;
while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
/** @var \Magento\Framework\App\RouterInterface $router */
foreach ($this->_routerList as $router) {
try {
$actionInstance = $router->match($request);
if ($actionInstance) {
$request->setDispatched(true);
$this->response->setNoCacheHeaders();
if ($actionInstance instanceof \Magento\Framework\App\Action\AbstractAction) {
$result = $actionInstance->dispatch($request);
} else {
$result = $actionInstance->execute();
}
break;
}
} catch (\Magento\Framework\Exception\NotFoundException $e) {
$request->initForward();
$request->setActionName('noroute');
$request->setDispatched(false);
break;
}
}
}
\Magento\Framework\Profiler::stop('routers_match');
if ($routingCycleCounter > 100) {
throw new \LogicException('Front controller reached 100 router match iterations');
}
return $result;
}
正如您在此dispatch()
方法中所看到的,路由器列表将循环以查找与此请求匹配的路由器列表。如果它找到该请求的控制器操作,则将调用并执行该操作。
在frontend/adminhtml上创建自定义路由
在这部分中,我们将使用一个简单的模块Mageplaza_HelloWorld。请按照上一篇文章了解如何在Magento 2中创建和注册模块。
我们将找到如何创建前端路由,管理路由以及如何使用路由重写控制器。
frontend路由
要注册前端路由,我们必须创建一个routes.xml文件:
文件: app/code/Mageplaza/HelloWorld/etc/frontend/routes.xml
<?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">
<!--Define a custom route with id and frontName-->
<route frontName="helloworld" id="helloworld">
<!--The module which this route match to-->
<module name="Mageplaza_HelloWorld"/>
</route>
</router>
</config>
请查看代码,您将看到注册路由非常简单。您必须使用标准路由器作为前端。此路由将有一个子节点,为其定义模块和2个属性:
- id属性是一个唯一的字符串,用于标识此路由。您将使用此字符串声明此模块操作的布局句柄
- frontName属性也是一个唯一的字符串,它将显示在url请求中。例如,如果您声明这样的路线:
<route frontName="helloworld" id="helloworld">
该模块的URL应该是:
此操作的布局句柄是:helloworld_controller_action.xml
因此,使用此示例路径,您必须在此文件夹中创建操作类:{namespace}/{module}/Controller/{Controller}/{Action}.php
adminhtml 路由
此路由与前端路由相同,但您必须在adminhtml文件夹中将其声明为路由器ID为admin
。
文件: app/code/Mageplaza/HelloWorld/etc/adminhtml/routes.xml
<?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 'admin' for admin route -->
<router id="admin">
<!--Define a custom route with id and frontName -->
<route id="mageplaza_helloworld" frontName="mageplaza_helloworld">
<!--The module which this route match to-->
<module name="Mageplaza_HelloWorld"/>
</route>
</router>
</config>
管理页面的url与前端页面的结构相同,但是admin_area
之前会添加名称route_frontName
以识别这是一个管理路由器。例如,admin cms页面的url:
管理页面的控制器操作将添加到文件夹内Controller/Adminhtml
。例如,对于以上网址:
{namespace}/{module}/Controller/Adminhtml/{Controller}/{Action}.php
使用route重写控制器
在这条路径中,我们将看到如何用路由器重写控制器。如上面的路径,您可以看到每个路由都有一个id属性来识别。那么如果我们定义具有相同id属性的2路由会发生什么?
答案是控制器操作将在两个模块中找到。Magento系统在配置模块排序顺序之前/之后提供属性,该顺序定义了首先找到的模块控制器。这是控制器重写的逻辑。
例如,如果我们想重写控制器customer / account / login,我们将在route.xml中定义更多路由,如下所示:
文件: app/code/Mageplaza/HelloWorld/etc/frontend/routes.xml
<?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">
<!--Define a custom route with id and frontName-->
<route frontName="helloworld" id="helloworld">
<!--The module which this route match to-->
<module name="Mageplaza_HelloWorld"/>
</route>
<route id="customer">
<module name="Mageplaza_HelloWorld" before="Magento_Customer" />
</route>
</router>
</config>
和控制器文件: app/code/Mageplaza/HelloWorld/Controller/Account/Login.php
所以frontController首先在我们的模块中找到Login动作,如果找到它,它将运行并且不会运行Magento_Customer的Login动作。我们成功地重写了一个控制器。
您也可以使用它来使第二个模块与另一个模块具有相同的路由器。例如,通过上述声明,您可以使用路由'customer'作为控制器操作。如果您有控制器'博客'和操作'Index.php',您可以使用此网址:
Routing 为 Magento 2 一个重要的部分,本文介绍基本应用的更多相关文章
- 微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同。
微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同. .NET Collection 函数库的 Has ...
- Magento添加一个下拉登陆菜单Create Magento Dropdown Login in a few minutes
Dropdown login forms are not a feature many online stores use, but in some cases they could be quite ...
- 一个分门别列介绍JavaScript各种常用工具的脑图
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:一个分门别列介绍JavaScript各种常用工具的脑图.
- 编写Java程序,读取文本文档的内容,去除文本中包含的“广告”字样,把更改后的内容保存到一个新的文本文档中
查看本章节 查看作业目录 需求说明: 读取文本文档的内容,去除文本中包含的"广告"字样,把更改后的内容保存到一个新的文本文档中 实现思路: 在main() 方法中,使用 new F ...
- Magento 自定义一个搜索功能
最近工作中有一个需求是需要做一个搜索的功能,但是因为需要定制一些外观,所以就不用传统的方法来继承基类GRID.实现这个需求的核心其实就是下面这个方法. $this->getLayout()-&g ...
- Magento 中一个订单的“生命历程”
当我们在网上愉快的买买买的时候, 你知道在这些屏幕“背后”正在进行着什么吗? 1. 当一个产品被加入到购物车后, 实际上发生了什么? 当第一个产品被加入到购物车, 系统首先会生成一个 quote (q ...
- 一个Monad的不严谨介绍
一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已,这有什么难以理解的?* 之前了解了下Monad,后来一段时间没碰,最近研究Parser用到Monad时发现又不懂了.现在重新折腾,趁着 ...
- WPF: WpfWindowToolkit 一个窗口操作库的介绍
在 XAML 应用的开发过程中,使用MVVM 框架能够极大地提高软件的可测试性.可维护性.MVVM的核心思想是关注点分离,使得业务逻辑从 View 中分离出来到 ViewModel 以及 Model ...
- Grafana是一个可视化面板-安装配置介绍
Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Prometheus和Ope ...
随机推荐
- selenium2 run in Jenkins GUI testing not visible or browser not open but run in background浏览器后台运行不可见
http://wiki.hudson-ci.org/display/HUDSON/Tomcat Tomcat from Windows GUI Testing in Windows Most Wi ...
- Got a packet bigger than‘max_allowed_packet’bytes错误的解决方法
通常项目上线前都有一些初始化数据需要导入,在今天博客系统发布前我使用sqlyog工具远程登录服务器的Mysql数据库,执行sql脚本对初始数据进行导入的时候报错: Got a packet bigge ...
- SQLServer之函数简介
用户定义函数定义 与编程语言中的函数类似,SQL Server 用户定义函数是接受参数.执行操作(例如复杂计算)并将操作结果以值的形式返回的例程. 返回值可以是单个标量值或结果集. 用户定义函数准则 ...
- Oracle 12c RAC 静默安装文档
参考文档: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/cwlin/index.html https://docs. ...
- luajit官方性能优化指南和注解
luajit是目前最快的脚本语言之一,不过深入使用就很快会发现,要把这个语言用到像宣称那样高性能,并不是那么容易.实际使用的时候往往会发现,刚开始写的一些小test case性能非常好,经常毫秒级就算 ...
- Linux(Centos7)下搭建SVN服务器 (转载)
系统环境:centos7.2 第一步:通过yum命令安装svnserve,命令如下: yum -y install subversion 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停 ...
- SQLServer之CHECK约束
CHECK约束添加规则 1.CHECK 约束用于限制列中的值的范围. 2.Check约束通过逻辑表达式来判断数据的有效性,用来限制输入一列或多列的值的范围,在列中更新数据时,所要输入的内容必须满足Ch ...
- django url分发,视图,模板回顾
Django基础轮廓 MTV+controller 一 url分发系统: 1 简单使用 url(r'^articles/2003/$', views.special_case_2003), # spe ...
- admin组件
Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它 ...
- vue2.0 :style :class样式设置
HTML :style 的用法 <el-dialog custom-class="creatUser-wrap" :style="{display:formShow ...