创建layout布局文件 布局文件的命名规则为:<Router Name>_<Controller Name>_<Action Name>
这里我们的布局文件名称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文件,继续来新建这个模板文件。 创建模板文件
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 创建Model模型-Model |--Job.php |--ResourceModel |--Job.php |--Job |--Collection.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'); } }
代码内容如下: <?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'); } }
代码内容如下: <?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
代码内容如下: <?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(); } } (责任编辑:最模板) |