1
0

More than 3 years have passed since last update.

Magento2でページを作成する

Last updated at Posted at 2021-03-29

今回は任意のURLでページを表示させるようエクステンションを改修していきます。

ファイル準備

今回は以下の2ファイルを作成します。

  • magento/app/code/Vendor/News/etc/frontend/routes.xml
  • magento/app/code/Vendor/News/Controller/News/Index.php

作成後のディレクトリ構成は以下です。

magento/
   └ app/
      └ code/
         └ Vendor/
            └ News/
               ├ Controller/
               │  └ News/
               │     └ Index.php
               ├ etc/
               │  └ frontend/
               │     └ routes.xml
               │  └ module.xml
               └ registration.php

ファイル作成

このファイルでエクステンションにURLを割り当てます。standardはフロントエンドを指し、frontNameにURLを指定します。

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="standard">
        <route id="vendor" frontName="vendor">
            <module name="Vendor_News"/>
        </route>
    </router>
</config>

コントローラーを記述します。以下はdev.magento.com/vendor/news/indexにアクセスした時excute()が実行されます。

Index.php
<?php

namespace Vendor\News\Controller\News;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    public function execute()
    {
        echo 'ニュース一覧';
    }
}

ページの表示

それでは確認します。

$ php bin/magento setup:upgrade

このURLにアクセスしてページが表示され「ニュース一覧」と出力されていればページの表示は完了です。
http://dev.magento.com/vendor/news/index

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0