Website icon Fixit

Creating Magento Modules

How is a Magento Extension created?;

Ένας σημαντικός παράγοντας στη πλατφόρμα Magento, καθοριστικός για τη λειτουργία του, είναι οι επεκτάσεις ή modules. Σε προηγούμενο άρθρο αναφέρεται η σημασία και όσα πρέπει να γνωρίζει κανείς για τις επεκτάσεις. Στο άρθρο “How important are Magento Modules?;” βρίσκονται αναλυτικές πληροφορίες. Στη πραγματικότητα, το κόστος των επεκτάσεων, είναι καθοριστικό για την εξέλιξη ενός καταστήματος Magento και στρέφονται κάποιοι στη δημιουργία εξατομικευμένων επεκτάσεων. Πόσο όμως εύκολο είναι αυτό και ποια τα οφέλη αλλά και οι παγίδες που ίσως κρύβονται;

What is a Magento extension?;

A Magento extension is a package of code that is responsible for specific functionality in Magento. Technically, it is a directory containing PHP and XML files in blocks, controllers, helpers, and models that are related to a specific business feature. While both a module and an extension are similar in theory, the term module often refers to core code, i.e. code that is part of Magento itself. In contrast, an extension refers to a component that can extend and customize Magento functionality. Extensions can be distributed and installed across multiple stores to help achieve specific functionality.

Magento scalability.

Over the years, the main focus behind Magento's development has been to maximize its extensibility. At the core of Magento's development model is the practice of extending the core code. This strategy helps maintain the Magento core code and allows merchants and developers to customize Magento to their requirements. Magento's code follows most of the PSR2 Coding Standards for PHP. 

In addition, Magento is also based on many well-known architectural structures and incorporates recognized and tested software structures called design patterns. Of these, MVC (model-view-controller) is the one most relevant to developing an extension. Using these structures and design patterns helps PHP developers navigate development issues that could arise while developing extensions. 

By placing code in modules, an extension is self-contained, making it possible to modify or replace it without negatively affecting other areas of the code. The purpose of any extension is to provide specific features by extending existing functionality or implementing new ones. Finally, designing each extension to work independently mitigates the risk of one extension conflicting with another and allows them to work in parallel.

Magento extensions are crucial to its operation.

Types of Magento extensions.

How to create a custom extension in Magento 2.

Magento 2 extension development is big news in the market. Already Magneto 2 has helped thousands of e-commerce stores by providing superior experiences to their customers. It is more mobile-friendly than Magento 1. Magento 2 has been developed to support PWA (Progressive Web Applications). It also acts as a highly developed platform that provides: powerful marketing, smooth catalog management tools, visual merchandising, stage and preview updates, and search engine optimization. Recently, Magento 2 extension development has been carried out.

  1. The latest Magento 2 version must be installed on the system. 
  2. Magento cache should be disabled.
  3. Developer mode selection: Allows checking all errors.

The creation of Magento Modules opens up new avenues for development.

Steps for developing Magento Module.

Step 1: Create extension files and folders for installation and registration. Create the folder and extension: 

  app/code/Fixit/HelloWorld

In case the code folder does not exist, it can be created manually. Here, the extension name is Helloworld and the extension namespace is the Fixit folder. 

Τώρα με έναν άλλο κωδικό, πρέπει να δημιουργηθεί ένα αρχείο module.XML στο φάκελο “Helloworld”. Για να εγγραφεί αυτή η ενότητα, πρέπει να δημιουργηθεί ένα αρχείο register.php στον ίδιο φάκελο helloworld. Μετάβαση στο Magento 2 root και, στη συνέχεια, εκτέλεση της εντολής αναβάθμισης (upgrade command). 

  app/code/Fixit/HelloWorld/etc/module.xml

And the content:

<?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=”Fixit_HelloWorld” setup_version=”1.0.0″>
    </module>
</config>

Step 2: Create etc/registration.php file

  app/code/Fixit/HelloWorld/registration.php

And the content:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Fixit_HelloWorld’,
__DIR__
);

Step 3: Turn on the unit:

  php bin/magento module:enable Fixit_HelloWorld

Η ενότητα θα πρέπει να είναι διαθέσιμη τώρα. Μετά από αυτό το βήμα, όταν ανοίγει ο ιστότοπος στο πρόγραμμα περιήγησης, θα ληφθεί ένα σφάλμα λέγοντας “Αναβαθμίστε τη βάση δεδομένων σας”:

  php bin/magento setup:upgrade

Then, because the website will appear broken:

php bin/magento setup:static-content:deploy

After the deployment is complete, the extension is visible from the backend in System Configuration -> Advanced -> Disable Modules Output. Now, a controller will be created for module testing. But before, a route for the HelloWorld module will be created. Routes in magento are divided into 3 parts: Route Address Name, Controller and Action like the following example:

http://fixit.com/index.php/frontname/controller/action

To add a route, a routes.xml file must be created:

app/code/Fixit/HelloWorld/etc/frontend/routes.xml

since this is a frontend path, it is added to the frontend/ folder otherwise it should be added to the adminhtml/ folder

The content:

<?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 frontName=”helloworld” id=”helloworld”>
            <module name=”Fixit_HelloWorld”/>
        </route>
    </router>
</config>

It should create another folder:

app/code/Fixit/HelloWorld/Controller/Index/Test.php

and content:

<?php
namespace Fixit\HelloWorld\Controller\Index;

class Test extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory)
{
$this->_pageFactory = $pageFactory;
return parent::__construct($context);
}

public function execute()
{
echo “Hello World”;
exit?;
}
}

The Url must have this format:

http://<yourhost.com>/helloworld/index/test

 

 

 

Δημιουργήθηκε η πρώτη επέκταση Magento, η οποία όταν ανοιχτεί εμφανίζει: “Hello World!”. Ωστόσο το πεδίο των επεκτάσεων magento είναι τεράστιο και υπάρχουν πολλά για να μάθει κανείς ώστε να προχωρήσει στην ανάπτυξη τους. 

 

Exit mobile version