在Magento 2中创建管理员菜单

在Magento 2中创建管理员菜单

  • 第1步:创建menu.xml
  • 第2步:添加菜单项
  • 第3步:刷新Magento缓存

第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后端添加一个菜单项。我们将看到它的结构:

<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"/>
我们来解释一些属性:
  • id属性是此注释的标识符。它是一个唯一的字符串,应遵循以下格式:{Vendor_ModuleName} :: {menu_description}。
  • title属性是将在菜单栏上显示的文本。
  • module属性定义了该菜单所属的模块。
  • sortOrder属性定义了菜单的位置。较低的值将显示在菜单顶部。
  • parent属性是其他菜单节点的Id。它会告诉Magento这个菜单是另一个菜单的孩子。在这个例子中,我们有parent =“Mageplaza_HelloWorld :: helloworld”,所以我们知道这个菜单“Manage Posts”是“Hello World”菜单的子项,它将显示在Hello World菜单中。
  • action属性将定义此菜单链接到的页面的URL。如上所述,网址将遵循此格式{router_name} {controller_folder}{action_name}。 - 在此示例中,此菜单将链接到模块HelloWorld,控制器Post和action Index
  • resource属性用于定义管理员用户必须具有的ACL规则,以便查看和访问此菜单。我们将在其他主题中找到有关ACL的更多详细信息。

您还可以创建更多子菜单,它将显示为上面的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

posted @ 2018-07-15 20:30  徐锅  阅读(641)  评论(0编辑  收藏  举报