Magento2模块开发是magento开发人员必备的技能,此片文章中我们将一步一步教您在Magento2中创建一个HelloWorld模块,

基本要求

在Magento 2中启动模块开发过程之前,请确保满足以下要求:

  • 安装最新版本的Magento 2,即2.1(如果您使用的是旧版本)。
  • 如果您不想在每次更改编码时从Web存储中删除缓存,则应禁用Magento缓存。这将节省您的时间并加快您的模块开发过程。要禁用缓存,您需要转到Admin » System » Cache Management » 选择所有缓存类型并禁用它们。
  • 将Magento切换到开发人员模式。这将帮助您轻松查看网站中的所有错误和漏洞。要打开开发人员模式,您需要访问终端并在Magento 2根目录执行如下命令:
    php bin/magento deploy:mod:set developer

现在,让我们开始在Magento 2中创建新的自定义模块。

1.创建模块文件夹

在Magento 2中创建模块的过程与我们在旧版Magento中使用的过程完全不同。在Magento 2中,没有代码池文件夹。并且所有模块都按命名空间分组并放在app/code文件夹中。这意味着您需要在app/code目录中直接创建模块:app/code/<Vendor>/<ModuleName>

1.1模块设置

要设置模块,我们需要创建模块文件夹和其他基本文件。这一特定步骤将帮助您轻松地为最新版本的Magento注册新模块

创建这两个文件夹:

app/code/webstore

app/code/Webstore/HelloWorld

注意:这里'Webstore'是模块的命名空间,而'Helloworld'是模块的名称。

1.1.2创建module.xml文件以声明模块

创建模块文件夹后,您需要使用以下代码在app/code/Webstore/HelloWorld/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="webstore_HelloMagento" setup_version="1.0.0" />
</module>
</config>

这将在模块etc目录中创建一个配置,让Magento 2轻松识别模块的名称和版本。

在此特定文件中,我们将声明一个名为webstore_Helloworld且版本为1.0.0 的模块

1.1.3注册模块

在Magento 2中,您需要通过Magento ComponentRegistrar类在Magento系统中注册所有模块。因此,您需要在app/code/Webstore/Helloworld文件夹中创建registration.php文件:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'webstore_Helloworld',__DIR__);

1.1.4启用模块

注册模块后,您可以在Magento 2环境中启用它。要检查Magento是否已识别新模块,您需要运行以下命令行:

php bin/magento module:status1

上面的步骤将显示以下结果:

List of disabled modules:
Webstore_HelloWorld

这意味着系统已识别该模块,但未在Magento系统中启用。为此,您需要运行以下命令来启用该模块:

php bin/magento module:enable Webstore_HelloWorld

这将启用系统中的模块,并显示如下结果:

The following modules has been enabled:
- Webstore_HelloWorld

由于您是第一次在系统中启用此模块,因此请确保运行此命令以让Magento轻松检查和升级模块数据库:

php bin/magento setup:upgrade

注意:进入Admin » Stores » Configuration » Advanced » Advanced以检查新模块的可用性

2.创建控制器

2.1为新模块创建routes.xml文件

在此步骤中,您需要通过在Magento根目录中创建routes.xml文件来为新模块定义路由器。在app/code/Webstore/HelloWorld/frontend文件夹中添加以下代码:

<?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="helloworld" frontName="helloworld">
<module name="Webstore_Helloworld"/>
</route>
</router>
</config>

注意:上面的代码将帮助您定义前端路由器和路由HelloWorld

在Magento 2中,frontName属性成为URL的第一部分。这意味着您的URL将生成如下:

<frontname>/<controller_folder-name>/<controller_class_name>

举个例子:

helloworld/index/index

2.2创建index.php控制器文件

在此步骤中,您将需要创建控制器和action的index.php控制器文件来显示HelloWorld模块。为此,在app/code/Webstore/HelloWorld/Controller/Index文件夹中添加以下代码:

<?php

namespace Webstore\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action 
{
    protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory){
        $this->_resultPageFactory = $resultPageFactory; 
        parent::__construct($context); 
    }

    public function execute() {
        $resultPage = $this->_resultPageFactory->create(); 
        return $resultPage; 
    }
}

3.创建一个Block块

你需要创建一个block累,使用getHelloWorldTxt()方法,然后返回“Hello world” 字符串

3.1创建HelloWorld.php文件

app/code/Webstore/HelloWorld/Block 文件夹下创建HelloWorld.php,并写入以下代码

<?php

namespace Webstore\Helloworld\Block;   

class Helloworld extends \Magento\Framework\View\Element\Template 
{
    public function getHelloWorldTxt(){ 
        return'Hello world!'; 
    } 
}

4.创建一个布局和模板文件

在Magento 2中,您可以在模块内的视图文件夹中找到布局文件和模板。在此文件夹下,您可以看到三个子文件夹:

  • adminhtml:用于后端文件
  • frontend文件夹:用于前端文件,和
  • Base文件夹:用于admin和frontend文件。

因此,首先,我们将在前端布局中创建布局和模板文件。

4.1创建helloworld_index_index.xml文件

请在app/code/Webstore/Helloworld/view/forntend/layout文件夹中创建布局文件helloworld_index_index.xml并写入以下代码:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
<body>
<referenceContainer name="content">
<block class="Webstore\Helloworld\Block\Helloworld" name="helloworld" template="helloworld.phtml"/>
</referenceContainer>
</body>
</page>

注意:确保将块添加到内容容器并将块的模板设置为helloworld.phtml,然后创建下一步。

4.2在前端模板文件夹中创建helloworld.phtml文件

app/code/Webstore/Helloworld/view/frontend/templates文件夹中创建模板文件helloworld.phtml并写入下面代码:

<h1><?php echo $this->getHelloWorldTxt(); ?></h1>

注意: $this变量被视为块类,调用方法为getHelloWorldTxt()。

现在,在浏览器中访问/helloworld/index/index URL以在Magento 2中查看您的全新模块。该URL将如下所示:http://xyz.com/helloworld/index/display

如果您仔细遵循上述步骤,在Magento 2中创建新模块可能是一个简单的过程。您可以轻松使用我们的源代码并为您的Magento网上商店开发一流的模块。


想要为您的Magento2平台增加更多特色功能模块,请提交您的需求