在Magento 2中创建管理员菜单
在Magento 2中创建管理员菜单
第1步:创建menu.xml创建名为:menu.xml文件的管理菜单文件 app/code/Mageplaza/HelloWorld/etc/adminhtml/menu.xml
具有以下内容: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> <menu> </menu> </config>
第2步:添加菜单项<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> <menu> <add id="Mageplaza_HelloWorld::helloworld" title="Hello World" module="Mageplaza_HelloWorld" sortOrder="51" resource="Mageplaza_HelloWorld::helloworld"/> <add id="Mageplaza_HelloWorld::post" title="Manage Posts" module="Mageplaza_HelloWorld" sortOrder="10" action="mageplaza_helloworld/post" resource="Mageplaza_HelloWorld::post" parent="Mageplaza_HelloWorld::helloworld"/> <add id="Mageplaza_HelloWorld::hello_configuration" title="Configuration" module="Mageplaza_HelloWorld" sortOrder="99" parent="Mageplaza_HelloWorld::helloworld" action="adminhtml/system_config/edit/section/helloworld" resource="Mageplaza_HelloWorld::helloworld_configuration"/> </menu> </config>
在这个例子中,我们将创建一个名为“Hello World”的0级菜单和两个名为“Manage Posts”和“Configuration”的子菜单。menu.xml文件将定义一个“添加”注释的集合,该注释将向Magento后端添加一个菜单项。我们将看到它的结构:
我们来解释一些属性:
您还可以创建更多子菜单,它将显示为上面的Store菜单。 我想谈谈顶级菜单上的图标。您可以在0级菜单标题上方看到它们。此图标由Magento中的“管理图标”字体生成。您可以在此链接中查看所有图标以及如何创建图标 第3步:刷新Magento缓存确保管理员菜单项显示在Magento 2管理员上,您应该尝试刷新Magento 2缓存。 运行以下命令行: php bin/magento cache:clean
? 创建路由文件 ?创建文件:app / code / Tutorial / SimpleNews / etc / adminhtml / routes.xml(目的:将在此处声明模块的后端的路由器)并将以下代码插入其中: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/ Framework/App/etc/routes.xsd"> <router id="admin"> <route id="simplenews" frontName="simplenews"> <module name="Tutorial_SimpleNews" /> </route> </router> </config>
为每个菜单项添加角色 创建文件:app / code / Tutorial / SimpleNews / etc / adminhtml / routes.xml(目的:将在此处声明模块的后端的路由器)并将以下代码插入其中: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/ Framework/Acl/etc/acl.xsd"> <acl> <resources> <resource id="Magento_Backend::admin"> <resource id="Tutorial_SimpleNews::simplenews" title="Simple News" sortOrder="100"> <resource id="Tutorial_SimpleNews::add_news" title="Add News" sortOrder="1" /> <resource id="Tutorial_SimpleNews::manage_news" title="Manage News" sortOrder="2" /> <resource id="Tutorial_SimpleNews::configuration" title="Configurations" sortOrder="3" /> </resource> <resource id="Magento_Backend::stores"> <resource id="Magento_Backend::stores_settings"> <resource id="Magento_Config::config"> <resource id="Tutorial_SimpleNews::system_config" title="Simple News Section" /> </resource> </resource> </resource> </resource> </resources> </acl> </config>
运行以下命令行:更新 一下? php bin/magento cache:clean
访问后台查看权限: System?>?Permissions?>?User Roles (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |