D1M Magento Day2

140 阅读1分钟

后台部分

一、目录结构

Lvmh
|-- Dk
    |
    |-- Controller
    |   |-- Adminhtml
    |       |-- Ad
    |           |-- Index.php
    |-- etc
    |   |-- adminhtml
    |   |   |-- menu.xml
    |   |   |-- routes.xml
    |   |-- module.xml
    |-- view
    |   |-- adminhtml
    |       |-- layout
    |       |   |-- lvmh_dk_ad_index.xml
    |       |-- templates
    |           |-- helloworld.phtml
    |-- composer.json
    |-- registration.php
Lvmh(项目名称)
|-- Dk(模块名称)
    |
    |-- Controller(必须)
    |   |-- Adminhtml(必须)
    |       |-- Ad(必须,名字自定义)
    |           |-- Index.php(必须,名字自定义)
    |-- etc
    |   |-- adminhtml(必须)
    |   |   |-- menu.xml(必须)
    |   |   |-- routes.xml(必须)
    |   |-- module.xml(必须)
    |-- view(必须)
    |   |-- adminhtml(必须)
    |       |-- layout(必须)
    |       |   |-- lvmh_dk_ad_index.xml(必须,名字需要规则)
    |       |-- templates
    |           |-- helloworld.phtml
    |-- composer.json
    |-- registration.php(必须)

二、页面三大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="Lvmh_Dk::root" title="DkDk11" translate="title" module="Lvmh_Dk" sortOrder="15" resource="Lvmh_Dk::main"/>
        <add id="Lvmh_Dk::main" title="Panel" module="Lvmh_Dk" sortOrder="900" dependsOnModule="Lvmh_Dk" parent="Lvmh_Dk::root" resource="Lvmh_Dk::main"/>
        <add id="Lvmh_Dk::ads" title="Blogs" module="Lvmh_Dk" sortOrder="1" parent="Lvmh_Dk::main" action="lvmh_dk/ad/index" resource="Lvmh_Dk::ads"/>
    </menu>
</config>
  • id:一般设置为模块名称+自定义
  • resource:在ack.xml设置,下边会有介绍到
  • parent:层级
  • action
    • front_name/controller_path/action,其中
      • front_name在路由定义到
      • controller_path,这里的Adaction,就是类名Index(首字母小写??)
|-- Controller(必须)
    |   |-- Adminhtml(必须)
    |       |-- Ad(必须,名字自定义)
    |           |-- Index.php(必须,名字自定义)
    • 比如,二级菜单action设置为lvmh_dk/ad/index,我们后台点击action得url为http://magento243p2studydemo.local/admin_wuj1jh/lvmh_dk/ad/index/key/ed83fc6d3d4ad32ffd657e08bd11cfe2950a575b16564dc01c5706dd19152d26/,其中lvmh_dk/ad/index就为action

那么,该action是如何绑定模块或者路由的呢,看以下routes.xml

routes.xml

routes.xml对应的就是menu.xmlaction

<?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="lvmh_dk" frontName="lvmh_dk">
            <module name="Lvmh_Dk" before="Magento_Backend"/>
        </route>
    </router>
</config>

我还是以http://magento243p2studydemo.local/admin_wuj1jh/lvmh_dk/ad/index/key/ed83fc6d3d4ad32ffd657e08bd11cfe2950a575b16564dc01c5706dd19152d26/举例

  • id="admin"admin_wuj1jh
  • frontName="lvmh_dk":上边有提到
  • module:代表route id="lvmh_dk"对应Lvmh_Dk模块

ack.xml(待说明)

<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_Backend::system">
                    <resource id="Lvmh_Dk::main" title="Lvmh Dk" sortOrder="230">
                        <resource id="Lvmh_Dk::ads" title="Dk List" sortOrder="10"/>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

lvmh_dk_ad_index.xml

便是页面展示了,命名规则:routeId_controller_action.xml,首字母小写

参考