Magento2 后台菜单和路由

2018年12月31日 浏览量 31 3 min read
Magento2 后台菜单和路由

在这篇博客中,我们将看到如何为您的模块添加顶部菜单和后台路由

要将选项添加到后台顶部菜单中,我们需要创建文件menu.xml

Magease/Hello/etc/adminhtml/menu.xml

magento2中的后台菜单有3个部分
1.左栏中显示的主管理菜单标题
2.子菜单标题
3.实际菜单

因此,在我们的menu.xml中,我们需要至少3个条目才能完成这项工

<?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="Magease_Hello::hello" title="Hello" module="Magease_Hello" sortOrder="15" dependsOnModule="Magease_Hello" resource="Magease_Hello::hello"/>
        <add id="Magease_Hello::hello_world" title="World" module="Magease_Hello" sortOrder="10" parent="Magease_Hello::hello" resource="Magease_Hello::hello" />
        <add id="Magease_Hello::hello_world_test1" title="Test1" module="Magease_Hello" sortOrder="10" parent="Magease_Hello::hello_world" action="hello/world" resource="Magease_Hello::hello_world_test1"/>
    </menu>
</config>

需要注意的要点
1.id:应该是唯一的
2.sortOrder:可用于更改左侧菜单中的排序
3.resource:用于定义acl,我们将在下面看到
4.parent:用于设置父项菜单,如果不是父级,则其表现为顶级菜单

接下来,我们需要在Magease/Hello/etc/acl.xml文件中添加条目

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Adminhtml::admin">
                <resource id="Magease_Hello::hello" title="Hello" sortOrder="20">
                    <resource id="Magease_Hello::hello_world" title="World" sortOrder="10">
                        <resource id="Magease_Hello::hello_world_test1" title="Test1" sortOrder="10">
                        
                        </resource>
                    </resource>
                </resource>
                <!-- below part of our system configuration settings -->
                <resource id="Magento_Adminhtml::stores">
                    <resource id="Magento_Adminhtml::stores_settings">
                        <resource id="Magento_Adminhtml::config">
                            <resource id="Magease_First::test_config" title="Hello World Section" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

完成这些步骤后,如果您打开后台,您应该会看到自定义侧面菜单和子菜单项。

侧边栏中的每个菜单项都有一个自定义图标。如果你的菜单需要一个自定义项,请在你的css文件中添加类.admin__menu .item-hello> a:before并编写你的自定义css。

后台路由

如上所述,在我们的routes.xml中,我们添加了动作'hello/world',现在我们需要为它添加路由。

后台的路由与前端相似,差异很小。

首先,我们需要创建一个routes.xml文件

Magease/Hello/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"> <!-- This part is different from frontend -->
        <route id="hello" frontName="hello">
            <module name="Magease_Hello" />
        </route>
    </router>
</config>

这里我们命名路由为“hello”,接下来我们需要创建控制器

Magease/Hello/Controller/Adminhtml/World/Index.php

因为我们的控制方法是“hello/world”,所以我们添加World控制器和Index控制方法

控制器文件中的代码是

<?php

namespace Magease\Hello\Controller\Adminhtml\World;

class Index extends \Magento\Backend\App\Action
{
   
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        echo __METHOD__;
        exit;
    }
}

如果正确遵循所有步骤,当您单击管理菜单“Test1”时,您应该会看到一个白页,其中包含“Magease\Hello\Controller\Adminhtml\World\Index::execute”文本

如果出现了一些错误,那么您将自动重定向到仪表板。

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1