Magento: Difference between revisions
| Line 189: | Line 189: | ||
====EAV Model==== | ====EAV Model==== | ||
* Entity Attribute Value (EAV) | * Entity Attribute Value (EAV) | ||
* Each attribute is assigned a backend type (varchar, int, text …). | |||
* The list of entities can be found in the DB table 'eav_entity_type', the list of attributes are in DB table 'eav_attribute' with reference to 'eav_entity_type' | * The list of entities can be found in the DB table 'eav_entity_type', the list of attributes are in DB table 'eav_attribute' with reference to 'eav_entity_type' | ||
Revision as of 15:08, 25 August 2018
General
- Magento 2 was released in 2015
- Variants
- Magento Open Source (before 2018 Magento Community Edition)
- Magento Commerce (before Magento Enterprise Edition)
- Differences see here
- object oriented
- uses design patterns (GoF) like GRASP, but in an individual manner
- uses MVC with thin controller approach (Business Logic is mainly in Model or View), View is configured by layout XML
- user groups:
- web business user
- system administrator
- web API user
Technology
- PHP 7 and coding standards PSR-0 to PSR-4
- MySQL 5.6
- Apache 2.2, 2.4
- PHP Composer
- HTML5
- LESS CSS
- jQuery
- RequireJS
- Zend Framework
- Symfony
- Magento Testing Framework
- ...
Coding Standards
PHP
- PSR-1
- each sub-word should be capitalised in both instances, with classes having an initial upper-case letter (StudlyCaps) and methods an initial lower-case letter (camelCase)
- Class constants MUST be declared in all upper case with underscore separators
- PSR-2
- PSR-4
Architecture
- layers:
- presentation
- service
- domain
- persistence
Persistence Layer
- consists of
- layouts
- blocks
- templates
- controllers
Service Layer
- bridges presentation and domain layer
- provides service contracts (PHP interface, REST/SOAP API): a service contract is a set of PHP interfaces that is defined by a module. This contract comprises data interfaces and service interfaces.
Business Layer
- implementents the business logic
- modul communication via event observers, pluginsa and di.xml
Persistance Layer
- implements CRUD (create, read, update, delete) requests
- there are 3 types of classes:
- model classes: don't contain any code for communicating with the DB
- resource classes: read and write to the DB
- collection classes: array of individual model instances which implement IteratorAggregate and Countable
- models and resources are often seen as an unified thing called model
- there are simple resource model and Entity-Attribute-Value (EAV) resource model
Modules
A Magento Module is a directory with subdirectories containing blocks, controllers, helpers, and models that are needed to create a specific store feature. It has a life cycle that allows them to be installed, deleted, or disabled. Modules typically live in the vendor directory of a Magento installation, in a directory with the following PSR-0 compliant format: vendor/<VENDORNAME>/<TYPE>-<MODULENAME>, where <TYPE> can be one of the following values:
- module - for modules (module-customer-import-export)
- theme - for frontend and admin themes (theme-frontend-luma or theme-adminhtml-backend)
- language - for language packs (language-de_de)
or you can just create the app/code/<VENDORNAME>/<TYPE>-<MODULENAME> directory (<MODULE_ROOT_DIR>) and the required directories within it. Inside the <MODULE_ROOT_DIR> you will find all the code related to this module:
- <MODULE_ROOT_DIR>/registration.php
module.xml
- <MODULE_ROOT_DIR>/etc/module.xml declares the module by configuration like name, version, dependencies, ...
<config>
<module name="<VENDORNAME>_<MODULENAME>" setup_version="<VERSION>">
[<sequence>
<module name ="<VENDORNAME>_<MODULENAME>" />
<DEPENDENCY_LIST>
</sequence>]
</module>
</config>
config.xml
- <MODULE_ROOT_DIR>/etc/config.xml
- the values of the attributes are somehow the 'reset to factory values', they are used if no corresponding DB entry is found in core_config_data with path=[carriers|???]/<CODENAME>/<ATTRIBUTE>
...
<default>
<carriers> // carries is for shipping, other options ???
<<CODENAME>> // same as $_code property in model class
<<CONFIG_ATTRIBUTE>><SYSTEM_DEFAULT_VALUE></<CONFIG_ATTRIBUTE>> // name used in system.xml and in code
<active><0|???></active> // active or not
<model>
<FILEPATH_FILENAME_WO_EXT> // model class with same name as FILENAME
</model>
<title><TEXT></title>display in the frontend
<sallowspecific><0|1></sallowspecific> // for carries: available in all countries or only in selected countries
<sort_order><sort_order>
</<CODENAME>>
</carriers>
</default>
system.xml
- <MODULE_ROOT_DIR>/etc/adminhtml/system.xml
- used for Magento system configuration
...
<system>
...
<section id=["carriers"|???]>
...
<group>
<label><LABEL></label>
<field id="<CONFIG_ATTRIBUTE>" type="<TYPE>"> // for TYPEs see /magento/framework/Data/Form/Element/Renderer/Factory.php row 26 or here
<label><LABEL></label>
[<validate><VALIDATE>{ <VALIDATE>}</validate>] // see below
[<source_model><MODEL></source_model>]
[<frontend_class><CLASS></frontend_class>]
[<comment><COMMENT></comment>] // will be displayed in small letters below the field
[<tooltip><TOOL_TIP></tooltip>]
[<backend_model><CLASS_NAMESPACE></backend_model>] // e.g. used for server side validation
</field>
...
</group>
</section>
</system>
VALIDATE :=
'validate-no-html-tags' => 'HTML tags are not allowed'
'validate-select' => 'Please select an option.'
'validate-no-empty'
'validate-alphanum-with-spaces'
'validate-street'
'validate-phoneStrict'
'validate-phoneLax'
'validate-fax'
'required-entry' => 'This is a required field.'
'validate-number' => 'Please enter a valid number in this field.'
'validate-digits' => 'Please use numbers only in this field. Please avoid spaces or other characters such as dots or commas.'
'validate-date' => 'Please enter a valid date.'
'validate-email' => 'Please enter a valid email address. For example johndoe@domain.com.'
'validate-emailSender'
'validate-password'
'validate-admin-password'
'validate-url' => 'Please enter a valid URL. Protocol is required (http://, https:// or ftp://)'
'validate-clean-url'
'validate-xml-identifier'
'validate-ssn'
'validate-zip-us'
'validate-date-au'
'validate-currency-dollar'
'validate-not-negative-number' => 'Please enter a number 0 or greater in this field.'
'validate-zero-or-greater' => 'Please enter a number 0 or greater in this field.'
'validate-state' => 'Please select State/Province.'
'validate-cc-number' => 'Please enter a valid credit card number.'
'validate-data' => 'Please use only letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'
- all elements below <system> have the following attributes:
- showInDefault=["0"|"1"], showInWebsite=["0"|"1"] and showInStore=["0"|"1"] which control whether the element is shown in the corresponding configuration level.
model classes
- module declaration in <MODULE_ROOT_DIR>/Model/ Not Validated, probably wrong: The _construct() method is implemented by Magento in some classes and it's called automatically inside the __construct function declaration, so if you are extending a Magento class like a Model you can use it to perform some stuff after object creation. In a Resource Model or Model Class you should define a _construct() method in order to define the table and the primary_key.
Simple Model
- <MODULE_ROOT_DIR>/Model/[<SUB_DIR>/]<MODELNAME>.php
class <MODELNAME> extends \Magento\Framework\Model\AbstractModel
{
protected function _construct()
{
$this->_init('<VENDORNAME>\<MODULENAME>\Model\ResourceModel\<MODELNAME>'); // tells Magento about resource class
- resource declaration in <MODULE_ROOT_DIR>/Model/ResourceModel/<MODELNAME>.php
class <MODELNAME> extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init('<TABLENAME>', '<PRIMARY_KEY_COLUMN>');
- create collection in <MODULE_ROOT_DIR>/Model/ResourceModel/<MODELNAME>/Collection.php
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('<FULL_MODEL_CLASSNAME>', '<FULL_RESOURCE_CLASSNAME>');
EAV Model
- Entity Attribute Value (EAV)
- Each attribute is assigned a backend type (varchar, int, text …).
- The list of entities can be found in the DB table 'eav_entity_type', the list of attributes are in DB table 'eav_attribute' with reference to 'eav_entity_type'
- <MODULE_ROOT_DIR>/Model/<MODELNAME>.php
class <MODELNAME> extends \Magento\Framework\Model\AbstractModel
{
protected function _construct()
{
$this->_init('<VENDORNAME>\<MODULENAME>\Model\ResourceModel\<MODELNAME>'); // tells Magento about resource class
- resource declaration in <MODULE_ROOT_DIR>/Model/ResourceModel/<MODELNAME>.php
class <MODELNAME> extends \Magento\Framework\Model\Entity\AbstractEntity
{
protected function _construct()
{
$this->_read =
$this->_write =
}
public function getEntityType()
{
...
}
- create collection in <MODULE_ROOT_DIR>/Model/ResourceModel/<MODELNAME>/Collection.php
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('<FULL_MODEL_CLASSNAME>', '<FULL_RESOURCE_CLASSNAME>');
Themes
Modules and themes are the units of customization in Magento. Themes strongly influence user experience and storefront appearance.
Caching
- located in /var/ directory
Code Generation
- Factory, Proxy and Interceptor classes are generated and stored in /var/generation/
DB Tables
- core_config_data: e.g. data from config.xml
- setup_module: all modules and their versions
Areas
Customer
- Magento 2.x applies the new SHA-256 hashing algorithm over the old MD5 in Magento 1. SHA-256 stands for Secure Hash Algorithm which is used in Magento 2 to hash passwords.
- The Magento_Customer module uses the EAV structure to store customer data. Thus, there is no single table that stores customer information. Rather, multiple tables exist, depending on the customer property and its data type.
Shipping
Object Model
Magento\Framework\DataObject
- implements getXYZ() and setXYZ() by overriding the PHP __call magic method
Magento\Quote\Model\Quote\Address\RateRequest
This class contains all information about items in cart/quote, weight, shipping address and so on.
\Magento\Framework\Exception\
Magento\Framework\Exception\LocalizedException
The ctor expects as a first parameter a Magento\Framework\Phrase object which can be created by the __() function defined in \app\functions.php and which is used to JSONize the information.
Magento\Shipping\Model\Carrier\AbstractCarrier
Base class for shipping with major methods for shipping models.
Directory Structure
- /app/code: for local developed modules
- /app/design/frontend: for storefront themes
- /app/design/adminhtml: for admin themes
- /app/i18n: for language packages
- /var/generation: contains only? generated files
- /var/?: cached infos
Operations
Commands
<MAGENTO_INSTALL_DIR>\bin\php magento module:status <MAGENTO_INSTALL_DIR>\bin\php magento module:enable <MODULE_NAME> // adds module to /app/etc/config.php
Upgrade
<MAGENTO_INSTALL_DIR>\bin\php magento setup:upgrade // triggers setup scripts of all modules that needs setup, if the module has one, and updates version info in table setup_module <MAGENTO_INSTALL_DIR>\bin\php magento setup:db-schema:upgrade <MAGENTO_INSTALL_DIR>\bin\php magento setup:db-data:upgrade
In Magento 2 upgrade is defined by the following rule (see here):
if (No 'schema_version' in table 'module_setup) then InstallSchema else UpgradeSchema RecurringSchemaEvent if (No 'data_version' in table 'module_setup) then InstallData else UpgradeData RecurringDataEvent
Deployment
<MAGENTO_INSTALL_DIR>\bin\php magento deploy:mode:show <MAGENTO_INSTALL_DIR>\bin\php magento deploy:mode:set developer
Magento Marketplace
- UweHeuerAccessKey
- Public Key: 6f7df3e4e393b84da74c41937990b227
- Private Key: a39505396b6112f47c43aa8c565cad8b
Installation
Sources
- Magento Marketplace
- free videos
- many Magento 2 articles
- Magento DevDocs
- PHP Developer Guide @ Magento DevDocs
- German Demo Shop
- Kindle Book 'Magento 2 Developer's Guide'