Magento2 모듈을 만드는 방법

11003 단어 PHPMagento2

모듈 목적


모듈의 목적은 새로운 기능을 실현하거나 다른 모듈의 기능을 확장함으로써 특정한 제품 기능을 제공하는 것이다.각 모듈은 독립적으로 작동하도록 설계되어 있기 때문에 특정 모듈의 포함이나 배제는 일반적으로 다른 모듈의 기능에 영향을 주지 않는다.

Magento2 모듈을 위한 모듈 폴더 만들기


모듈 이름은 두 부분으로 되어 있습니다.
1. 공급업체명: 회사명[Karabiner] 또는 개인명[Moaz].
2. 모듈 명칭: 일반적으로 모듈의 목적을 나타내는 명칭이다.
공급업체 이름과 모듈 이름은 대문자로 된 낙타 껍질이 필요합니다VENDOR_NAMEKarabiner,MODULE_NAMEHelloMagneto2이다.
이것은 사용자 정의 모듈의 디렉터리[MAGENTO_DIRECTORY]/app/code/[VENDOR_NAME]/[MODULE_NAME]입니다.
폴더 이름은 [MAGENTO_FOLDER]/app/code/Karabiner/HelloMagneto2입니다.
*MAGENTO_DIRECTORY: 환경에서 magento2의 디렉토리입니다.

module.xml 만들기


Magento 2 will use it to recognize the module’s name and module’s version
모듈의 etc 디렉터리에 module.xml 파일을 만듭니다.Magento2는 이 식별 모듈의 이름과 버전을 사용합니다.app/code/[VENDOR_NAME]/[MODULE_NAME]/etc/module.xml모듈 이름과 버전을 설정하기 위해 다음 코드를 추가합니다.
모듈 이름: HelloMangento2버전: 0.0.1app/code/Karabiner/HelloMagento2/etc/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="Karabiner_HelloMagento2" setup_version="0.0.1" />
</config>
모듈이 다른 모듈<sequence>에 의존하는 경우 탭을 사용하여 구성 요소module.xml 파일에 읽기 순서를 지정하여 모듈을 불러올 때 필요한 파일을 다른 모듈에서 읽었는지 확인할 수 있습니다.
app/code/Karabiner/HelloMagento2/etc/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="Karabiner_HelloMagento2" setup_version="0.0.1" >
       <sequence>
            <!-- Karabiner_HelloMagento2 is dependent on Vendor_ModuleA: -->
            <module name="Vendor_ModuleA" />
            <!-- you can add more than one module-->
        </sequence>
    </module>
</config>

registration.php 로그인 모듈로


각 모듈의 루트 디렉터리에서registration.php라는 파일이 필요합니다.registration.php는 모듈의 입구점이다.
app/code/Karabiner/HelloMagento2/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
$registrar = new ComponentRegistrar();

if ($registrar->getPath(ComponentRegistrar::MODULE, "Karabiner_HelloMagento2") === null) {
    ComponentRegistrar::register(ComponentRegistrar::MODULE, "Karabiner_HelloMagento2", __DIR__);
}

모듈 폴더의 구조는 다음과 같다.
app/code/Karabiner/
└── HelloMagento2
    ├── etc
    │   └── module.xml
    └── registration.php

모듈 활성화 및 설치


명령줄에서 모듈을 설치할 수 있습니다.
터미널을 열고 이 명령을 사용하십시오.

잘못된 모듈을 표시하려면:


MAGENTO_DIRECTORY
cd [MAGENTO_DIRECTORY]
php bin/magento module:status
==========output==========
List of enabled modules:
....
List of disabled modules:
Karabiner_HelloMagento2 <- not enable yet

모듈 사용:


MAGENTO_DIRECTORY
cd [MAGENTO_DIRECTORY]
php bin/magento module:enable Karabiner_HelloMagento2
==========output==========
The following modules have been enabled:
- Karabiner_HelloMagento2

구성 모듈:


모듈이 활성화된 경우 다음을 설정해야 합니다.
MAGENTO_DIRECTORY
cd [MAGENTO_DIRECTORY]
php bin/magento setup:upgrade

비활성화 모듈:


MAGENTO_DIRECTORY
cd [MAGENTO_DIRECTORY]
php bin/magento module:disable Karabiner_HelloMagento2
==========output==========
The following modules have been disabled:
- Karabiner_HelloMagento2
Cache cleared successfully.

모듈 루트 만들기


Magento2는 다음 형식의 URL을 사용합니다.
http://[magento_url]/[frontName]/[controller_name]/[action_name]
따라서 routers.xml는 다음과 같이 문서를 제작한다.
app/code/Karabiner/HelloMagento2/etc/frontend/routes.xml
다음은 다음 코드를 추가합니다.
app/code/Karabiner/HelloMagento2/etc/frontend/routes.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard"> <!-- standard or admin -->
    <route id="HelloMagento2" frontName="custom_router_name">
        <module name="Karabiner_HelloMagento2" />
    </route>
</router>
</config>
다음은 routes.xml의 구성을 설명한다.
1.router태그
이것은 이 공유기가 전방 공유기에 추가된다는 것을 의미한다.standard 라우터가 관리 라우터에 추가된다는 의미입니다.
2. admin 레이블
-frontName 이름은 URL 뒤에 있습니다.
frontName이 route인 경우 URL은 custom_router_name입니다.
-id Magento에서 이 경로의 고유 노드 ID를 지정합니다.이것은 레이아웃과 관련된 XML 파일 이름의 첫 번째 부분이기도 합니다.(routeId_controller_action.xml)
예:http:///custom_router_name/index/index
따라서 앞으로 컨트롤러와 동작을 만들기 전에 모듈의 라우터 이름을 설정해야 합니다.

컨트롤러 및 작업 작성


브라우저에 표시할 URL을 만듭니다.
“Hello From karabiner Magento 2 module something action”.
작업 파일을 작성합니다.http://<magento_url>/custom_router_name/코드를 추가합니다.
다음은namespace입니다.app/code/Karabiner/HelloMagento2/Controller/Something/Index.php /VENDOR_NAME/MODULE_NAME\Controller\[Controller_Class]app/code/Karabiner/HelloMagento2/Controller/Something/Index.php
<?php
namespace /Karabiner/HelloMagento2\Controller\Something;

class Index extends \Magento\Framework\App\Action\Action
{
  public function __construct(
\Magento\Framework\App\Action\Context $context)
  {
    return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello Form karabiner magento 2 module something action'; 
    exit;
  }
}
app/code/Karabiner/
app/code/Karabiner/
└── HelloMagento2
    ├── Controller
    │   ├── Index #http://<magento_url>/custom_router_name/index
    │   │   ├── Index.php #http://<magento_url>/custom_router_name/index/index
    │   │   └── View.php #http://<magento_url>/custom_router_name/index/view
    │   └── Something 
    │       └── Index.php #http://<magento_url>/custom_router_name/somethhing/index
    ├── etc
    │   ├── frontend
    │   │   └── routes.xml
    │   └── module.xml
    └── registration.php
After adding new route we should clear cache
MAGENTO_DIRECTORY
bin/magento cache:clean
==========output==========
Cleaned cache types:
config
layout
block_html
collections
reflection
db_ddl
compiled_config
eav
customer_notification
config_integration
config_integration_api
google_product
full_page
config_webservice
translate
vertex

좋은 웹페이지 즐겨찾기