服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

Magento2.x 如何创建一个Module?(3)

时间:2016-12-16 10:44来源:未知 作者:最模板 点击:
upgrade表结构UpgradeSchema.php File:app/code/Silk/Test/Setup/UpgradeSchema.php 代码内容如下: ?phpnamespace Silk\Test\Setup;use Magento\Framework\Setup\UpgradeSchemaInterface;use Magento\Fra

upgrade表结构UpgradeSchema.php

File:app/code/Silk/Test/Setup/UpgradeSchema.php

代码内容如下:

<?php

namespace Silk\Test\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;


/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) 
        {
            $installer->startSetup();
            $tableName = $setup->getTable('silk_test');
            if ($setup->getConnection()->isTableExists($tableName) == true) 
            {
                $installer->getConnection()
                ->addColumn($installer->getTable('silk_test'),'e_profile_picture', array(
                    'type'      => Table::TYPE_TEXT,
                    'nullable'  => true,
                    'length'    => 255,
                    'comment'   => 'Profile Picture'
                )); 
            }            
        }
        
        $installer->endSetup();

    }
    
}

备注: 更新表结构要查看module.xml中的schema_version标签的值是否小于这里的版本号.

创建后台配置文件

后台的配置文件是在etc/adminhtml/目录下创建,比如menu.xml,routes.xml,system.xml,di.xml等等,依次来解释下这些xml的作用

menu.xml

menu.xml可以看作是配置当前模块在后台的导航位置的配置信息

File:app/code/Silk/Test/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="Silk_Test::job_head" title="Test" module="Silk_Test" sortOrder="100" parent="Magento_Backend::stores" resource="Silk_Test::job_head" />
        <add id="Silk_Test::job" title="Test" module="Silk_Test" sortOrder="20" parent="Silk_Test::job_head" action="test/job" resource="Silk_Test::job" />
    </menu>
</config>

说明:

  • parent: 'Magento_Backend::stores"表示当前模块位于后台Stores选项中;

  • title : 表示在后台显示的名称;

  • id : 唯一标识符;

  • action: 表示点击该选项要跳转的路由;

acl.xml

File:app/code/Silk/Test/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_Backend::content">-->
                <resource id="Silk_Test::job_head" title="Test" sortOrder="100" >
                    <resource id="Silk_Test::department" title="Departments" sortOrder="10">
                        <resource id="Silk_Test::department_save" title="Save Department" sortOrder="10" />
                        <resource id="Silk_Test::department_delete" title="Delete Department" sortOrder="20" />
                    </resource>
                    <resource id="Silk_Test::job" title="Test" sortOrder="20">
                        <resource id="Silk_Test::job_save" title="Save Job" sortOrder="10" />
                        <resource id="Silk_Test::job_delete" title="Delete Job" sortOrder="20" />
                    </resource>
                </resource>
                <!--</resource>-->

                <!-- Admin config -->
                <resource id="Magento_Backend::stores">
                    <resource id="Magento_Backend::stores_settings">
                        <resource id="Magento_Config::config">
                            <resource id="Silk_Test::test" title="Test Section" />
                        </resource>
                    </resource>
                </resource>

            </resource>
        </resources>
    </acl>
</config>

查看权限位置:System > Permissions > User roles;

routes.xml

routes.xml是配置后台路由的信息

File:app/code/Silk/Test/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="test" frontName="test">
            <module name="Silk_Test" before="Magento_Backend" />
        </route>
    </router>
</config>

说明:

  • frontName: 表示你后台控制器的名称

  • id: 唯一标识符;

system.xml

system.xml是配置后台路由的信息

File:app/code/Silk/Test/etc/adminhtml/system.xml

代码内容如下:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="test" translate="label" sortOrder="1000">
            <label>Test</label>
        </tab>
        <section id="test" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Test</label>
            <tab>test</tab>
            <resource>Silk_Test::test</resource>
            <group id="department" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Test configuration</label>
                <field id="view_list" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Show job list</label>
                    <comment>Show list of the viewing </comment>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容