magento 2模块开发实例helloworld模块

1、在app/etc/config.php中添加自定义的模块(magento1声明模块是在app/etc/modules):

 'Silk_Helloworld' =>1,

值为1,开启模块,类似true,0关闭模块
2、创建module.xml:app/code/Silk/Helloworld/etc/module.xml

 <?xml version="1.0" encoding="UTF-8" ?>
  <module name="Silk_Helloworld" setup_version="2.0.0" /> 

3、创建前段控制器:app/code/Silk/Helloworld/ect/fronted/routers.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">
        <route id="helloworld" frontName="helloworld">
            <module name="Silk_Helloworld" />
        </route>
    </router>
</config>

4、创建一个控制器:appcodeSilkhelloworldControllerIndexindex.php

namespace Silk\Helloworld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
       $this->getResponse()->appendBody('HELLO WORLD');
    }
}

5、创建文件app/code/Silk/Helloworld/registration.php:

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Silk_Helloworld',/*namespace/module*/
    __DIR__
);

6、创建文件app/code/Silk/Helloworld/composer.json:
{

 "name": "silk/helloworld",/*namespace/module*/
  "description": "silk",/*namespace*/
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/framework": "100.0.*",
    "magento/module-ui": "100.0.*",
    "magento/module-config": "100.0.*",
    "magento/module-contact": "100.0.*"
  },
  "type": "magento2-module",
  "version": "100.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "extra": {
    "map": [
      [
        "*",
        "Silk/Hellworld"/*namespace/module*/
      ]
    ]
  },
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "silk\\helloworld\\": ""/*namespace/module*/
    }
  }
}

7、执行命令:php bin/magento setup:upgrade
xampp环境使用该命令方法:
(1)、打开shell,输入cd htdocsmagento,回车
(2)、输入上面的命令,等待一会即可.
linux环境:
php bin/magento setup:upgrade
8、在浏览器中输入silk.magento2.cn/helloworld.显示:

helloworld模块创建成功了。
9、登录到后台,可以查看新建的helloworld模块: Stores > Configuration > Advanced > Advanced.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
What this book covers Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the command-line interface for an experienced webmaster. Chapter 2, Magento 2 System Tools, explains how to install Magento 2 via the command shell. Magento released a new powerful tool to manage and install sample data, reindex your database, back up your site, or flush your caches, which are just a few of the options. Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give your store a performance boost. There are also external services that you can use as a cache. Chapter 4, Creating Catalogs and Categories, shows you one of the major elements of a Magento store before creating products. Creating the correct product type including attributes is an important step in setting up a Magento store. Chapter 5, Managing Your Store, covers setting up the correct tax rules, configuring an inventory, and creating customer groups. Chapter 6, Creating a Magento 2 Theme, discusses the Magento 2 blank theme and how to use the fallback to create seasonal variations. It also explains how the new theme is set up and where files are stored. Chapter 7, Creating Magento 2 Extensions – the Basics, contains the basic functions required to use extensions in a Magento 2 installation. It contains a brief introduction to new methods introduced in the Magento 2 framework and examples on how to create basic functions. Chapter 8, Creating Magento 2 Extensions – Advanced, explains how to use advanced features in extensions for Magento 2. It also includes how to add unit/functional tests as this is a new requirement for extensions listed on the new Magento
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值