A quick reference guide for Magento2, command line, xml snippets, template file snippets, Models and other useful bits of information.
|
Model View Controller Pattern (MVC)
|
|
Design pattern where business, presentation and coupling logic are separated
|
|
Front Controller Pattern
|
|
Makes sure that there is one and only one point of entry. All requests are investigated, routed to the designated controller and then processed accordingly to the specification
|
|
Factory Pattern
|
|
Responsible of factorizing (instantiating) classes
|
|
Singleton Pattern
|
|
Checks the whether this class has already been instantiated before, this results in a shared instance.
|
|
Registry Pattern
|
|
Internal registry: a global scoped container for storing data
|
|
Prototype Pattern
|
|
It defines that instances of classes can retrieve a specific other class instance depending on its parent class (the prototype)
|
|
Object Pool Pattern
|
|
A box with objects so that they do not have to be allocated and destroyed over and over again
|
|
Iterator Pattern
|
|
The iterator pattern defines that there is a shared way to iterate over a container with objects
|
|
Lazy Loading Pattern
|
|
Lazy loading ensures that loading data is delayed until the point when it is actually needed
|
|
Service Locator Pattern
|
|
The service locator pattern abstracts away the retrieval of a certain service. This allows for changing the service without breaking anything
|
|
Module Pattern
|
|
An implementation of the module pattern would make sure that each element can be removed or swapped
|
|
Observer Pattern
|
|
By defining observers (or listeners), extra code can be hooked which will be called upon as the observed event fires
|
|
Proxy Pattern
|
|
Provide a surrogate or placeholder for another object to control access to it.
Use an extra level of indirection to support distributed, controlled, or intelligent access. Add a wrapper and delegation to protect the real component from undue complexity. |
|
A component declares itself (that is, defines its name and existence) in the module.xml file, located in the Magento install directory at <ComponentName>/etc/
|
|
<?xml version="1.0"?> |
|
Used to configure module. The responsibilities of the config.xml configuration file used in earlier versions of Magento is now divided between several files, located in various module directories.
|
|
|
Example
|
|
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"><default> |
|
What case we use di.xml ?
|
We can use di.xml for (rewrite) preference of a particular class.
|
|
We can send a new or replace the existing class arguments.
|
|
|
Use plugins to do some stuff before, after and around a function
|
|
|
By using virtualTypes creating a sub-class of another class.
|
|
|
Example for Preference
|
|
<preference for="Magento\Customer\Api\AddressRe positoryInterface" type="Magento\Customer\Model\Resour ceModel\AddressRepository" /> |
|
|
Above code, When someone asks to instantiate a Magento\Customer\Api\AddressRepositoryInterface it will instantiate a Magento\Customer\Model\ResourceModel\Addre ssRepository object (the type attribute).
|
|
|
Example for Arguments
|
|
<type name="Magento\Customer\Model\Resour ceModel\Group" shared="false"> <arguments> <argument name="groupManagement" xsi:type="object">Magento\Customer\ Api\GroupManagementInterface\Proxy< /argument> </arguments> </type> |
|
|
In the above code, We are sending object as an argument, we are saying system to insert "Proxy" class as an object with the name of groupManagement. Also we can use Arguments for replacing the existing argument too.
|
|
|
Example for Plugin
|
|
<type name="Magento\Customer\Model\Resour ceModel\Visitor"> <plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\ Log" /> </type> |
|
|
In the above code , public function clean($object) in visitor class is called after public function afterClean(Visitor $subject, $logResourceModel) which is in Log class.
|
|
|
Example for Virtual Types
|
|
<virtualType name="ourVirtualTypeName" type="Pulsestorm\TutorialVirtualType\Model\Argu ment1"> </virtualType> |
|
|
Creating a virtual type is sort of like creating a sub-class for an existing class. With virtual types, the only behavior you can change in your virtual sub-class is which dependencies are injected.
|
|
|
Custom events can be dispatched by simply passing in a unique event name to the event manager when you call the dispatch function. The unique event name is referenced in your module’s events.xml file where you specify which observers will react to that event.
|
|
|
The observer xml element has the following properties:
|
|
|
name (required)
|
The name of the observer for the event definition
|
|
instance (required)
|
The fully qualified class name of the observer
|
|
disabled
|
Determines whether this observer is active or not. Default value is false
|
|
shared
|
Etermines the lifestyle of the class. Default is false
|
|
Example
|
|
<event name="my_module_event_before"> |
|
|
file is where we define our module access control list resources. Access control list resources are visible under the Magento admin System | Permissions | User Roles area, when we click on the Add New Role button.
|
|
|
Example
|
|
<?xml version="1.0"?> |
|
The system.xml is a configuration file which is used to create configuration fields in Magento 2 System Configuration. You will need this if your module has some settings which the admin needs to set
|
|
A menu.xml file is a collection of <add/> nodes. Each of these nodes adds a Menu Item to Magento’s backend
|
|
|
Example
|
|
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> |
|
|
id
|
Attribute defines a unique identifier for this node. By convention, (but not required) this should the the name of the module
|
|
title
|
Attribute controls the text an end users sees for the Menu Item
|
|
module
|
Attribute should match the current module
|
|
sortOrder
|
Attribute controls how this Menu Item is sorted with the other Menu Items in the system
|
|
resource
|
attribute defines the ACL rule a user must have in order to access this Menu Item
|
|
The file that is responsible for widgets in Magento is placed in [module_dir]/etc/widget.xml
|
|
The webapi.xml is used to configure access rights and API Interface which specify method that will be used.
|
|
|
Example
|
|
<?xml version="1.0"?> |
|
|
Resource tag defines what resources user needs to have to be able to access this api call. Possible options are self, anonymous or Magento resource like Magento_Catalog::products or Magento_Customer::group
|
|
Cache Management
|
Commands
|
|
Clear all caches:
|
php magento cache:clean
|
|
Clearing a single cache:
|
php magento cache:clean config
|
|
Flush all caches:
|
php magento cache:flush
|
|
Flushing a single cache:
|
php magento cache:flush config
|
|
Index Management
|
Commands
|
|
Runs all available indexes:
|
php magento indexer:reindex
|
|
Cron
|
Commands
|
|
Runs all cron jobs by schedule:
|
php magento cron:run
|
|
Help & Knowledge
|
Commands
|
|
Display help/infos about a command
|
php magento help indexer:reindex
|
|
Lists all available CLI commands
|
php magento list
|
|
Developement & Production
|
Commands
|
|
Set to the production mode to harden your setup
|
php magento deploy:mode:setproduction
|
|
(exceptions are not displayed to the user, only logged to log files)
|
|
|
Developer mode is less secure than Production mode
|
php magento deploy:mode:setdeveloper
|
|
provides verbose logging (errors displayed in browser) enhanced debuggingand disables static view file caching
|
|
|
Recompile Store
|
Commands
|
|
Recompile the Magento 2 codebase
|
php magento setup:di:compile
|
|
How to get product collection in custom template block
|
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
|
|
How to use Mage::log method in Magento 2
|
|
protected $logger; |
|
|
You use debug, exception, system for psr logger for example
|
|
$this->logger->info($message); |
|
|
How to retrieve product information in Magento 2
|
|
|
In Magento 2 proposed to use service layer for this. Try use
\Magento\Catalog\Model\ProductRepository::getById method to get product by id |
|
|
How to get a store in Magento 2 programmatically?
|
|
|
Magento 1.x
|
|
Mage::app()->getStore() |
|
function rm_store($id = null) { |
|
|
How to make JavaScript strings translatable (localizable)
|
|
|
To make JavaScript strings translatable or localizable in Magento 2, use :
$.mage.__ |
|
|
Example : $.mage.__('Select type of option.')
|
|
|
Some Magento 2 core modules use another way to translate JavaScript strings: they define/require the mage/translate library
|
|
|
How to call theme images from a static block
|
|
|
You call theme images from static block as follows: {{view url=”images/demo.jpg”}}
|
|
|
How to instantiate a model in Magento 2
|
|
|
Magento strictly discourages the use of ObjectManager directly. It provides service classes that abstract it away for all scenarios.
For all non-injectables (models) you have to use factory: |
|
protected $pageFactory; |
|
|
How to get the сollection of custom Magento 2 modules
|
|
protected $mymodulemodelFactory; |
|
|
and you can use in any one of the class methods:
|
|
|
$collection = $this->mymodulemodelFactory->create();
|
|
|
How to get POST and GET requests in Magento 2
|
|
|
In a case of a controller that extends Magento\Framework\App\Action\Action, it is possible to get the request with the aid of
$this->getRequest()->getPost(). |
|
|
For a custom class, inject the request in the constructor:
|
|
namespace Namespace\Module\Something; |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.