使用示例页面将自定义菜单项添加到 magento 2 管理侧边栏中的销售菜单选项卡

Add custom menu item to sales menu tab in magento 2 admin sidebar with example page

我研究了Magento 2,不到2周,就出现了这样的任务。

我需要在销售侧边栏添加一个自定义标签,也许有人已经遇到过这样的任务?

要使用默认登录页面向 Magento 2 管理添加菜单项,请添加这些文件并根据您自己的需要进行调整。这将在带有示例页面的销售菜单选项卡下创建一个自定义菜单项。

app/code/Company/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_Module',
    __DIR__
);

app/code/Company/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Module"/>
</config>

app/code/Company/Module/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_Backend::admin">
                <resource id="Magento_Sales::sales">
                    <resource id="Magento_Sales::sales_operation">
                        <resource id="Company_Module::custommenu" title="Custom Menu" sortOrder="10"/>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

app/code/Company/Module/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>
        <add id="Company_Module::custommenu"
             title="Custom Menu"
             module="Company_Module"
             sortOrder="100"
             parent="Magento_Sales::sales_operation"
             action="custommenu/index/index"
             resource="Company_Module::custommenu"
        />
    </menu>
</config>

app/code/Company/Module/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">
    <router id="admin">
        <route id="custommenu" frontName="custommenu">
            <module name="Company_Module"/>
        </route>
    </router>
</config>

app/code/Company/Module/Controller/Adminhtml/Index/Index.php

<?php

namespace Company\Module\Controller\Adminhtml\Index;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory = false;

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

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Company_Module::custommenu');
        $resultPage->getConfig()->getTitle()->prepend(__('Custom Menu'));
        return $resultPage;
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Company_Module::custommenu');
    }
}

app/code/Company/Module/view/adminhtml/layout/custommenu_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" template="Company_Module::content.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Company/Module/view/adminhtml/templates/content.phtml

<p>Content goes here</p>

添加文件后,运行 从命令行执行以下命令:php bin/magento setup:upgrade