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

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

时间:2016-12-16 10:44来源:未知 作者:最模板 点击:
创建layout布局文件 布局文件的命名规则为:Router Name_Controller Name_Action Name 例如: local.magento2.com/test -----test_index_index.xml local.magento2.com/test/say -----test_say_

创建layout布局文件

布局文件的命名规则为:<Router Name>_<Controller Name>_<Action Name>

例如:
local.magento2.com/test -----test_index_index.xml
local.magento2.com/test/say -----test_say_index_index.xml
local.magento2.com/test/hello/world -----test_hello_world.xml

这里我们的布局文件名称test_hello_world.xml

File:app/code/Silk/Test/view/frontend/layout/test_hello_world.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" layout="2columns-right">
    <body>
        <referenceContainer name="content">
            <block class="Silk\Test\Block\Hello" name="hello" template="helloworld.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

这里我们又定义了一个helloworld.phtml文件,继续来新建这个模板文件。

创建模板文件

File:app/code/Silk/Test/view/frontend/templates/helloworld.phtml

helloworld.phtml代码内容如下:

<h2>HelloWorld</h2>
<p>Congratulations ! You have created your first Magento Module !</p>
<p>The block classname is : <?php echo get_class($block) ?></p>

现在重新刷新我们的网页URL:local.magento2.com/test/hello/world
到此,我们就完成了一个简单的前端控制器到模板的数据输出.需要注意的是,block仍然是提供给模板phtml页面数据,但是在模板中调用Block中的函数方式时,Magento2是用$block,而不再是$this.
下面我们继续完善我们这module,在创建后台部分之前,先来创建Model模型.

创建Model模型

-Model
   |--Job.php
   |--ResourceModel
          |--Job.php
          |--Job
              |--Collection.php

接着我们依次来创建这几个文件

File:app/code/Silk/Test/Model/Job.php

代码内容如下:

<?php
namespace Silk\Test\Model;

use \Magento\Framework\Model\AbstractModel;

class Job extends AbstractModel
{
    const JOB_ID = 'entity_id'; // We define the id fieldname

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'test';

    /**
     * Name of the event object
     *
     * @var string
     */
    protected $_eventObject = 'job';

    /**
     * Name of object id field
     *
     * @var string
     */
    protected $_idFieldName = self::JOB_ID;

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Silk\Test\Model\ResourceModel\Job');
    }
}

File:app/code/Silk/Test/Model/ResourceModel/Job.php

代码内容如下:

<?php
namespace Silk\Test\Model\ResourceModel;

use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Job extends AbstractDb
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        // Table Name and Primary Key column
        $this->_init('silk_job', 'entity_id');
    }

}

File:app/code/Silk/Test/Model/ResourceModel/Job/Collection.php

代码内容如下:

<?php
namespace Silk\Test\Model\ResourceModel\Job;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{

    protected $_idFieldName = \Silk\Test\Model\Job::JOB_ID;

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Silk\Test\Model\Job', 'Silk\Test\Model\ResourceModel\Job');
    }
}

到此,我们就创建了一个Model模型.接下来,我们来创建脚本文件,即Setup/目录下的文件

  • InstallSchema.php

  • UpgradeSchema.php

  • InstallData.php

  • UpgradeData.php

  • Recurring.php

备注:InstallSchema.php是模块初次运行时创建表结构的脚本,InstallData.php是模块初次运行时插入表中的数据,而UpgradeSchema.php和UpgradeData.php是根据module.xml中的setup_version版本号来执行更新表结构和表数据的脚本.

创建表结构InstallSchema.php

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

代码内容如下:

<?php

namespace Silk\Test\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $table = $installer->getConnection()->newTable(
            $installer->getTable('silk_test')
        )->addColumn(
            'e_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            array('identity' => true, 'nullable' => false, 'primary' => true),
            'Employee ID'
        )->addColumn(
            'e_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            array('nullable' => false),
            'Employee Name'
        )->addColumn(
            'e_address',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            '2M',
            array('nullable' => false),
            'Employee Address'
        )->addColumn(
            'is_active',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            array(),
            'Active Status'
        )->addColumn(
            'created_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            array(),
            'Creation Time'
        )->addColumn(
            'update_time',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            array(),
            'Modification Time'
        )->setComment(
            'Employee Table'
        );
        $installer->getConnection()->createTable($table);

        $installer->endSetup();

    }
}
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容