涵盖Magento模块开发的步骤

Magento connect. Modules are an integral part of Magento. These nifty little tools play an important role in solving a variety of tasks within the Magento platform with minimal efforts. Custom module development empowers online retailers in providing a rich and engaging shopping experience to their visitors by leveraging the benefits of advanced features in Magento.

Magento连接。 模块是Magento不可或缺的一部分。 这些精巧的小工具在毫不费力地解决Magento平台内的各种任务中发挥着重要作用。 定制模块开发使在线零售商能够利用Magento的高级功能的优势为访客提供丰富而引人入胜的购物体验。

Developing a custom module provides outstanding flexibility to design an effective e-commerce solution based on specific business requirements. When combined with the excellence of Magento, these custom modules make it easy for the online players to perfect tasks based on specific functionalities that are absolutely unique to their business. This way they are more equipped to discover the power of Magento and implement customized solutions that are fit to their needs.

开发定制模块提供了出色的灵活性,可以根据特定的业务需求设计有效的电子商务解决方案。 当结合Magento的卓越表现时,这些自定义模块使在线玩家可以轻松地根据其业务绝对独特的特定功能来完善任务。 这样,他们就更有能力发现Magento的力量并实施适合其需求的定制解决方案。

In this post, we are going to discuss about the ways through which we can easily develop custom modules and thus extend the abilities of our online store. But first understand the basic structure of Magento.

在这篇文章中,我们将讨论如何轻松开发自定义模块,从而扩展在线商店功能的方法。 但是首先要了解Magento的基本结构。

There are basically three kinds of Magento modules or extensions:

Magento模块或扩展基本上有三种:

  1. Core – The extension is presented by default in Magento and is available in the [magentodirectory]/app/code/core/Mage folder or directory.

    核心 –默认情况下,扩展名在Magento中显示,可在[magentodirectory] ​​/ app / code / core / Mage文件夹或目录中找到。

  2. Community – Available for free, the Community extension can be accessed from the Magento Connect website or any other community website. One can find this extension in the directory [magentodirectory]/app/code/community/[namespace]/[extensionname]

    社区 –社区扩展免费提供,可以从Magento Connect网站或任何其他社区网站访问。 可以在目录[magentodirectory] ​​/ app / code / community / [namespace] / [extensionname]中找到此扩展名

  3. Local – This extension or module is developed by third-party resources to perform some specific functions. You can find this extension in the folder or directory [magentodirectory]/app/code/local/[namespace]/[Extensionname].

    本地 –该扩展或模块由第三方资源开发,以执行某些特定功能。 您可以在文件夹或目录[magentodirectory] ​​/ app / code / local / [namespace] / [Extensionname]中找到此扩展名。

了解Magento模块的组件 (Understanding The Components of Magento Modules)

  • Blocks – Consist of several functions that allow you easily display data on templates.

    –由多个功能组成,可让您轻松地在模板上显示数据。

  • Models – Business logic of modules

    模型 –模块的业务逻辑

  • Resource Models – Contain functions that allows for database interaction

    资源模型 –包含允许数据库交互的功能

  • Controllers – Specifies page layouts and blocks and loaded when a URL is requested by the user.

    控制器 –指定页面布局和块,并在用户请求URL时加载。

  • Etc – Consists of configuration files in XML formats. It informs Magento about the number of modules and how they are going to interact.

    等等 –包含XML格式的配置文件。 它告知Magento有关模块的数量以及它们将如何交互的信息。

  • Helpers -functions that help you define business logics. These functions can be used anywhere across the Magento.

    助手 -帮助您定义业务逻辑的功能。 这些功能可以在Magento的任何地方使用。

  • SQL – integrates SQL scripts to help you create and modify SQL tables.

    SQL –集成SQL脚本以帮助您创建和修改SQL表。

为模块分配名称 (Assigning Name to Your Module)

First of all, we need to give an appropriate name to our module. Generally, the name of Magento modules are made of two parts: <Namespace>_<Module>. One of the most practical ways of assigning name to Magento module is to select <Namespace>, wherein you can either add author name or company name and in <module> you can add the actual module name.

首先,我们需要给我们的模块起一个适当的名字。 通常,Magento模块的名称由两部分组成:<名称空间> _ <模块>。 为Magento模块分配名称的最实用方法之一是选择<名称空间>,其中您可以添加作者名称或公司名称,在<模块>中可以添加实际的模块名称。

So, on the basis of above mentioned conditions, we are assigning name to our module as xyz_myMagentomodule. This name will be used as a reference in the entire article.

因此,根据上述条件,我们为模块分配名称为xyz_myMagentomodule。 该名称将在整篇文章中用作参考。

创建目录并配置它们 (Creating Directories and Configuring Them)

Here, we need to set up directories on the basis of what we have mentioned above. For this, simply navigate to your Magento installation direction and then go to app/code/local and start creating directories.

在这里,我们需要在上面提到的基础上建立目录。 为此,只需导航到您的Magento安装方向,然后转到app / code / local并开始创建目录。

Once you are done with this, it’s time to configure and activate the module that is xyz_myMagentomodule in the app/etc/modules directory. This is the directory where all the config files for Magento are stored.

完成此操作后,就该配置和激活app / etc / modules目录中的模块xyz_myMagentomodule了。 这是存储Magento的所有配置文件的目录。


<?xml version="1.0"?>
<config>
    <modules>
        <xyz_MyMagentomodule>
        <active>true</active>
        <codePool>local</codePool>
        </xyz_MyMagentomodule>
    </modules>
</config>

<?xml version="1.0"?>
<config>
    <modules>
        <xyz_MyMagentomodule>
        <active>true</active>
        <codePool>local</codePool>
        </xyz_MyMagentomodule>
    </modules>
</config>

Once you run the code, Magento will start detecting the location of your module. To enable it, go to <active> tag, and specify the value ‘true’ to activate the module. If everything is going well, then you can easily locate the module in the Magento Admin Panel > System > Configuration > Advanced > Advanced > Disable Modules Output list.

一旦运行代码,Magento将开始检测模块的位置。 要启用它,请转到<active>标签,并指定值'true'以激活该模块。 如果一切顺利,则可以轻松在Magento管理面板>系统>配置>高级>高级>禁用模块输出列表中找到该模块。

开发过程入门 (Getting Started with the Development Process)

Now, it’s time to create our module configuration file. This file will inform Magento about our module. For this, go to app/code/local/xyz/MyMagentomodule/etc and create a config.xml file and add the below mentioned code into it.

现在,该创建模块配置文件了。 该文件将告知Magento我们的模块。 为此,请转到app / code / local / xyz / MyMagentomodule / etc并创建config.xml文件,并将以下提到的代码添加到其中。


<?xml version="1.0"?>
<config>
    <modules>
        <xyz_myMagentomodule>
            <version>0.1.0</version>    <!-- This is the version number of your module -->
        </xyz_myMagentomodule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>xyz_myMagentomodule</module>
                    <frontName>myMagentomodule</frontName>
                </args>
            </mymodule>
        </routers>
    </frontend>
</config>

<?xml version="1.0"?>
<config>
    <modules>
        <xyz_myMagentomodule>
            <version>0.1.0</version>    <!-- This is the version number of your module -->
        </xyz_myMagentomodule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>xyz_myMagentomodule</module>
                    <frontName>myMagentomodule</frontName>
                </args>
            </mymodule>
        </routers>
    </frontend>
</config>

Now, let’s understand the significance of each tag.

现在,让我们了解每个标签的意义。

The first tag that is <module> you are noticing above contains the name and version number of your module. The version number is critical at the time of updating the module.

您在上面注意到的第一个标签是<module>,包含模块的名称和版本号。 在更新模块时,版本号至关重要。

Next is the <frontend> tag, the tag allows you keep Magento informed about the dispatched controls. Inside this tag, we have defined <routers>, which is helpful in defining the ways for accessing multiple controls with the help of routing function.

接下来是<frontend>标记,该标记使您可以使Magento知道已调度的控件。 在此标记内,我们定义了<routers>,这有助于定义通过路由功能访问多个控件的方式。

In the tag <myMagentomodule> we have defined the name we have given to our module. The <frontend> name is used to access the frontend of the module just like name of your website.com/index.php/mymodule/index.

在标签<myMagentomodule>中,我们定义了为模块指定的名称。 <frontend>名称用于访问模块的前端,就像您的website.com/index.php/mymodule/index的名称一样。

The reason why we are using name of your website.com/index.php/mymodule/index function because this way Magento will better look for the index actions of your module’s controller file. For creating the controller file, go to app/code/local/xyz/MyMagentomodule/controllers, and add the following code into it.

我们之所以使用您的website.com/index.php/mymodule/index函数的名称,是因为Magento可以更好地查找模块控制器文件的索引动作。 要创建控制器文件,请转到app / code / local / xyz / MyMagentomodule / controllers,然后将以下代码添加到其中。


<?php
class xyz_MyMagentomodule_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo "Hello World";
    }
}

<?php
class xyz_MyMagentomodule_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo "Hello World";
    }
}

Now, open the URL of name of your website.com/index.php/mymodule/index, it will display “Hello World”. Congrats! You successfully created your first Hello World Magento module.

现在,打开您的website.com/index.php/mymodule/index名称的URL,它将显示“ Hello World”。 恭喜! 您已成功创建了第一个Hello World Magento模块。

实施控制器调度 (Implementing Controller Dispatch)

Now, we have to extend the class i.e., Mage_Core_Controller_Front_Action, the class consists of all the function that allows you provide a specific path to your URL. The primary function of Magento class is to define the location of class file. This way, it’s clear that Magento class reflects the location of the class file. So the class Mage_Core_Controller_Front_Action is stored in Mage > Core > Controller > Front >Action.php.

现在,我们必须扩展该类,即Mage_Core_Controller_Front_Action,该类包含所有允许您提供URL特定路径的功能。 Magento类的主要功能是定义类文件的位置。 这样,很明显Magento类反映了类文件的位置。 因此,类Mage_Core_Controller_Front_Action存储在Mage> Core> Controller> Front> Action.php中。

So, the class name of our controller is xyz_MyMagentomodule_IndexController. Make sure your Magento name should be defined in a way that it gives you exact (<module>tag)_(Action Controllername)(keyword Controller).

因此,控制器的类名称为xyz_MyMagentomodule_IndexController。 确保您的Magento名称的定义方式应使其具有确切的(<module> tag)_(Action Controllername)(keyword Controller)。

Now, let me explain you the explanation of each and every tag.

现在,让我向您解释每个标签的解释。

  • <module> – The name of our module i.e., xyz_myMagentomodule

    <module> –我们模块的名称,即xyz_myMagentomodule
  • Action <controller name> – Index

    操作<控制器名称> –索引
  • Action controller along with a keyword

    动作控制器以及关键字

On the basis of this pattern, the name of our controller will be:

根据此模式,控制器的名称将为:

xyz_MyMagentomodule_IndexController

xyz_MyMagentomodule_IndexController

Now, the pattern of the URL will look like this: name of your website.com/index.php/frontendname/actionControllername/actionmethod

现在,URL的格式将如下所示:website.com/index.php/frontendname/actionControllername/actionmethod的名称

Based on this platform, the URL for our module will be:

基于此平台,我们模块的URL为:

name of your website.com/index.php/frontendname/actionControllername/actionmethod. Although you can access it using name of your website.com/index.php/mymodule because even if you don’t specify any action control or action method, Magento will still load the file by default.

您的website.com/index.php/frontendname/actionControllername/actionmethod的名称。 尽管您可以使用website.com/index.php/mymodule的名称来访问它,因为即使您未指定任何动作控件或动作方法,Magento仍将默认加载文件。

Lastly, let’s create a test action

最后,让我们创建一个测试动作


<?php
class xyz_MyMagentomodule_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo "Hello World";
    }
    public function testAction()  {
        echo "test action";
    }
}

<?php
class xyz_MyMagentomodule_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo "Hello World";
    }
    public function testAction()  {
        echo "test action";
    }
}

You can access the test action via yoursite.com/index.php/mymodule/index/test.

您可以通过yoursite.com/index.php/mymodule/index/test访问测试操作。

Hopefully, the tutorial has helped you in developing a custom module for Magento and implementing controlling dispatches.

希望本教程可以帮助您为Magento开发自定义模块并实现控制调度。

翻译自: https://www.script-tutorials.com/covering-the-steps-of-magento-module-development/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值