Magento2でエクステンションを作成する


今回からニュース一覧を表示するエクステンションを作成していきます。今回はエクステンションの作成と有効化までです。

エクステンションを作成する

準備として以下のファイルを作成します。(作成するエクステンション名はVendor_Newsです。)

  • magento/app/code/Vendor/News/etc/module.xml
  • magento/app/code/Vendor/News/registration.php
magento/
   └ app/
      └ code/
         └ Vendor/
            └ News/
               ├ etc/
               │  └ module.xml
               └ registration.php

作成したファイルにエクステンションの雛形を記述します。エクステンション名以外は全て共有です。

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="Vendor_News" setup_version="1.0.0"/>
</config>
registration.php
<?php
Magento\Framework\Component\ComponentRegistrar::register(
    Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_News',
    __DIR__
);

エクステンションを有効化する

ここまででMagentoがエクステンションとして認識するようになりました。確認してみましょう。

$ php bin/magento module:status

List of disabled modules:
Vendor_News

このように表示されれば認識されています。それではエクステンションを有効化します。

$ php bin/magento module:enable Vendor_News

エクステンションの作成と有効化は以上です。