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.
- Magento has an official marketplace called Magento Marketplace, where users of the platform can purchase and download third-party extensions and themes to extend their online stores. Magento has a robust Extension Quality Program that combines Magento expertise, development guidelines, and verification tools to ensure that all extensions in its marketplace meet coding standards and follow best practices.
- Out-of-the-box επεκτάσεις. Εκτός από τις επεκτάσεις στην αγορά του, το Magento διατίθεται με επιπλέον επεκτάσεις τρίτων. Αυτές οι επεκτάσεις ονομάζονται “Επεκτάσεις Πακέτο προμηθευτών”. Έχουν δοκιμαστεί διεξοδικά πριν συμπεριληφθούν σε οποιαδήποτε υποστηριζόμενη έκδοση του Magento βέβαια για τη ποιότητα τους. Μερικές από τις “Επεκτάσεις Πακέτο Προμηθευτών” που θα βρίσκονται συσκευασμένες με τις πιο πρόσφατες εκδόσεις Magento είναι το Amazon Pay, το dotdigital, το Klarna και το Vertex.
- Custom extensions. Many store owners often overuse third-party extensions, as they are cheap and easily accessible. However, overusing such extensions, and especially using poorly coded extensions, can hinder a store's performance. It is highly recommended to use custom extensions to avoid such an outcome. In cases where custom creation is not feasible, out-of-the-box third-party extensions should be tested before deploying to live stores and modified to use only the required functionality.
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.
- The latest Magento 2 version must be installed on the system.
- Magento cache should be disabled.
- 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 είναι τεράστιο και υπάρχουν πολλά για να μάθει κανείς ώστε να προχωρήσει στην ανάπτυξη τους.