Show Menu
Cheatography

Magento 2 Useful Methods Cheat Sheet by

Magento 2 is a robust and feature-rich e-commerce platform with numerous actions and functionalities. However, I can provide a condensed cheatsheet outlining key actions across different areas of Magento 2 development. Please note that this is a simplified overview, and you may need to refer to Magento 2 official documentation for more in-depth information.

Module Structure

app/co­de/­Ven­dor­Nam­e/M­odu­leName
Basic module structure
regist­rat­ion.php
Module regist­ration file
etc/mo­dul­e.xml
Module config­uration file
etc/fr­ont­end­/ro­ute­s.xml
Define frontend routes
etc/ad­min­htm­l/r­out­es.xml
Define admin routes

Controller Actions

execute()
Main controller action method
Result­Int­erface
Various result types (Json, Redirect, Page, etc.)
getReq­uest()
Retrieve request object
getRes­ponse()
Retrieve response object

Plugins

di.xml
Define plugin config­ura­tions
before­Met­hod(), around­Met­hod(), afterM­eth­od():
Plugin methods
proceed()
Call the next method in the chain

Magento CLI Commands

bin/ma­gento module­:enable Vendor­_Module
Enable a module
bin/ma­gento setup:­upgrade
Apply database schema and data changes
bin/ma­gento cache:­flush
Flushes cache including 3rd party modules
bin/ma­gento cache:­clean
Cleans Magento cache
bin/ma­gento indexe­r:r­eindex
Reindex Data

Models

load(), save(), delete()
Basic CRUD operations
__cons­truct()
Constr­uctor method
getData(), setData()
Get and set model data
getRes­ource()
Retrieve model's resource model

Layout XML

<re­fer­enc­eCo­nta­ine­r>, <re­fer­enc­eBl­ock>
Reference existing contai­ner­s/b­locks.
<block class=­"­" templa­te=­"­">
Difine a block
<ar­gum­ent­s>
Pass arguments to blocks
<update handle­="">
Apply layout updates.

Observers

events.xml
Define observer events
execut­e(­\Mag­ent­o\F­ram­ewo­rk­\Eve­nt­\Obs­erver $observer)
Observer method.
getEvent()
getEvent()

Templates

<?php echo $block­->e­sca­peH­tml­(__­("Te­xt")); ?>
Output escaped text
<?php echo $block­->g­etU­rl(­'ro­ute­/co­ntr­oll­er/­act­ion'); ?>
Generate URL
<?php echo $block­->g­etC­hil­dHt­ml(); ?>
Output child block content
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Magento 2 Cheat Sheet
          Magento CLI commands & Its usage Cheat Sheet

          More Cheat Sheets by Bhavi

          Magento CLI commands & Its usage Cheat Sheet