Home  >  Article  >  Backend Development  >  Develop custom modules for Magento

Develop custom modules for Magento

WBOY
WBOYOriginal
2023-08-31 21:49:07874browse

Magento Custom Module Development is a core part of any Magento development or Magento project as at any stage you may want to integrate your own functionality/modules into an existing Magento project.

In this series, I will cover the details of custom module development for Magento.

If you want to develop Magento further, check out the various useful Magento extensions on Envato Market.

为 Magento 开发自定义模块

In this series, I am referring to Magento Community Edition 1.7, albeit a custom version The module structure is the same in all versions of Magento. Before starting the actual module development, let’s quickly understand the basic structure of Magento.

Whenever you install a new Magento, you will notice the following Magento directory structure:

为 Magento 开发自定义模块

Magento MVC Structure Introduction

Like any other major framework like Joomla, CakePHP, CodeIgniter, etc., Magento also follows an MVC-based architecture, although this is slightly different from the core PHP MVC architecture. Here I will explain the differences in Magento architecture by comparing it with a simple PHP MVC architecture.

PHP MVC Architecture

In a typical MVC pattern, the flow of an application looks like this:

  1. There is one main entry point - index.php - from which the entire application routing mechanism is determined.
  2. Based on this routing mechanism and the requested URL pattern, the app will call the appropriate controller.
  3. The controller then calls the appropriate view.
  4. Finally, the view file collects data from the model file and displays the data.

Magento MVC Architecture

Magento’s MVC architecture adds several layers to the MVC pattern, but the basic control flow of the application is as follows:

  1. There is one main entry point - index.php - from which the entire application will be initialized.
  2. The appropriate controller will be called based on the requested URL.
  3. The controller defines pages and loads the layout files for these pages.
  4. The layout file tells the controller which block files to use.
  5. Block files collect data from model and helper files and pass it to template files.
  6. The template file receives the data and renders the html.

Initially, this may be difficult to understand as it contains some extra layers. To get more familiar with the control flow, let's develop a custom "Hello World" module.

Before you start using modules

  • I assume you already have version 1.7 or a working copy of Magento 1.7 (otherwise the version doesn't matter at this stage)
  • Disable caching. To disable caching, go to Magento Admin Panel > System > Cache Management > Select all cache types from the left checkbox > Select Action: Disable from the top right dropdown > Click Submit.

Magento module structure

Code Pool

Magento contains three types of code pools where all custom and core modules of Magento reside.

  1. The core pool contains all the core modules that come with Magento installation by default. These modules are written by Magento developers. It is recommended not to modify these modules because whenever you upgrade your Magento installation, all core modules will be overwritten and your modifications will be lost.
  2. The Community Pool contains all modules (i.e. custom modules) developed by third-party programmers and installed through Magento Connect. These modules typically extend the core modules and provide their own functionality, which can usually be used anywhere in Magento.
  3. The local pool contains all custom modules that will be used in a specific project but will not be read in Magento Connect

So we have two pool options: community pool or local pool. Since we are developing our own project, we will use a local pool, although there are no restrictions on using a community pool.

structure

Magento modules are composed of the following components:

  • BlockContains functions for displaying data in the template.
  • Model Contains the business logic of the module.
  • Resource ModelContains functions for database interaction.
  • Controller Define the page layout and block the file and load it when the URL is requested.
  • etcContains a configuration file in XML format that tells Magento how many files the module has and how the module interacts.
  • HelpersContains functions for defining common business logic (e.g. image resizing, validation). These functions can be used anywhere in your Magento application
  • sqlContains SQL scripts for creating, modifying, or dropping SQL tables.

Module naming

We need to name our module. Generally speaking, Magento module names consist of two parts: _. The best practice for naming your Magento modules is to choose <namespace></namespace> as the author or company name and <module></module> as the actual module name.

Based on these naming conventions, I gave our module the Chiragdodia_Mymodule name. We will refer to this name throughout this series.

Code setup and configuration

Let’s create a directory based on the above structure. Go to the Magento installation directory and navigate to app/code/local and create a directory as shown below.

为 Magento 开发自定义模块

Next, we will create the configuration file Chiragdodia_Mymodule.xml in app/etc/modules directory to configure and activate our module. This directory contains configuration files for all modules.

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

This file will tell Magento the location of our module. In the active tag, we specify true to enable our module. If everything is correct so far, you will find your module in the Magento Admin Panel > System > Configuration > Advanced > Advanced > Disable Module Output list. From here you can enable and disable your module.

Getting Started: Development

Next we will create the module configuration file. This file will tell Magento all the information about our module. This includes how many files our module contains, what types of files (models, helpers, database classes), etc.

Go to app/code/local/Chiragdodia/Mymodule/etc and create a config.xml Will contain the following contents of the file

<?xml version="1.0"?>
<config>
    <modules>
        <Chiragdodia_Mymodule>
            <version>0.1.0</version>    <!-- Version number of your module -->
        </Chiragdodia_Mymodule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Chiragdodia_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
    </frontend>
</config>

Let’s go through each tag line by line. Here, the first tag is <module></module> , which contains the name and version of our module. The version number is very important when updating modules.

The

<frontend></frontend> tag will tell Magento information about the scheduled controller. Inside the <frontend></frontend> tag, we define <routers></routers> which tells Magento how to access our controller through the routing mechanism.

In the <mymodule></mymodule> tag we are in <module></module> # Tags and frontend names in <frontname>#. We can access our module on the frontend by using the frontend name, for example </frontname>yoursitename.com/index.php/mymodule/index.

By calling

yoursitename.com/index.php/mymodule or yoursitename.com/index.php/mymodule/index Magento will look for the index action file for the module controller. Therefore, we need to create the controller file.

Go to

app/code/local/Chiragdodia/Mymodule/controllers and create the file IndexController.php Contains the following content.

Please note that in Magento, each file name and class name are case-sensitive. When creating files and classes, it's important to be careful about naming your creations.

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

Now open the URL

yoursite.com/index.php/mymodule/index It will print "Hello tuts World". Awesome - we finally finished our first hello world module.

Controller Scheduling

Here we extend the class

Mage_Core_Controller_Front_Action , which contains all methods used in URL routing. Magento class names reflect the location of the class files. Therefore, the class Mage_Core_Controller_Front_Action is located at location Mage > Core > Controller > Front > Action.php

Look at the class name of our controller, which is

Chiragdodia_Mymodule_IndexController. Magento controllers should be named in a manner that reflects (tag)_(Action Controllername)(keyword Controller).<module></module>

  • tag = Chiragdodia_Mymodule (We have defined this tag in config.xml) b>
  • Operation
  • Controller Name = Index
  • Operation controller followed by
  • Controller keyword
According to this pattern, the name of our controller is

Chiragdodia_Mymodule_IndexController

Now view the URL pattern that follows the routing pattern yoursite.com/index.php/frontendname/actionControllername/actionmethod

  • 前端名称 = mymodule
  • actionControllername = 索引
  • actionmethodname = 索引

根据此网址模式,我们模块的网址为 yoursite.com/index.php/mymodule/index/index。您还可以使用 yoursite.com/index.php/mymodule 访问它,因为只要您未指定 actionControlleractionmethod 名称,Magento 就会加载默认情况下的索引控制器和索引操作。

现在让我们再创建一个操作:testAction 。

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

我们可以使用 URL yoursite.com/index.php/mymodule/index/test 访问 testAction。 如前所述

  • 前端名称 = mymodule
  • actionControllername = 索引
  • actionmethodname = 测试

这就是控制器在 Magento 中的工作原理。

一开始,一次理解所有内容可能很困难,因此我已将所有源代码包含到此模块中,以便您可以在将其用作指南的同时查看它并实现自己的工作。

下一步是什么?

在下一部分中,我们将通过创建布局和块文件来填充模块中的一些布局。我们将了解布局文件在 Magento 中如何工作以及块在 Magento 上下文中扮演什么角色。

在此之前,请创建您自己的模块,并让我知道任何给您带来麻烦的事情。

The above is the detailed content of Develop custom modules for Magento. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn