Magento 2 Design patterns | pk magento 2

 My youtube channel : https://www.youtube.com/channel/UChb7DM9SspzrUVh4hnWL50A

REQUESTED BY : ANKUR PORWAL

01 (MVC)  MODEL VIEW CONTROLLER PATTERN (MVVM) MODEL VIEW VIEWMODEL PATTERN 
Model View Controller, MVC in short. This is a design pattern where business, presentation and coupling logic are separated. Magento utilizing a DOM based configuration layer. It is used xml to drive the configuration and actions of the application on top of the regular Model-View-Controller architecture. 
MVVM design pattern is used in magento on checkout page with knockoutjs. 

02 FRONT CONTROLLER PATTERN
The front controller pattern to implement workflows for our application. It has a single entry point (index.php) for all requests. It have bootstrap file which is decied which controller will call. With (module name) / (folder name) / (Controller class name) with psr-4 stander.  

03 FACTORY PATTERN
The factory Method is using to create class in Magento. We instantiate a class in Magento by calling an appropriate method passing an abstract name repre, jdsenting a class group followed by a class name. Class groups and their appropriate abstractions are declared in our configuration XML files in our module’s /etc/ folder.

04 SINGLETON PATTERN
This is same like factory class abstraction and class groups in Magento, the Singleton pattern is instantiated for Blocks and Classes. It is restricts the instantiate of a class to one object and it is useful to restrict the limited number of connections in a database or limited amount of memory for a particular instance of class.

05 REGISTRY PATTERN
The registry pattern is a pattern that allows any object or data to be available in a public global scope for any resource. All the singleton patterns are stored in the internal registry, a global scoped container for storing data. This pattern is not only for internal use.

06 PROTOTYPE PATTERN
The prototype pattern is used as an extension of the Abstract Factory pattern. It is ensures that an appropriate subclass is instantiated via appropriate types that are assigned to an object.

07 OBJECT POOL PATTERN
The object pool pattern is simply a box with objects so that they don't have to be allocated and destroyed over and over again. It is keeps objects ready for use over and over again instead of re-instantiating them and destroying them once finished. It is a great way to save on memory consumption.

08 ITERATOR PATTERN
The iterator pattern is a design pattern that allows specify an iterator and allow for multiple different sets of data to be passed without changing the underlying structure that allows the iteration. In Magento, this is handled by the Varien_Data_Collection which turn uses various baked-in PHP classes (like Array Iterator) for having a more Object Orient-interface to arrays. This is ensures that model-collections will always have a common API to iterate over without being dependent of the actual models.

09 LAZY LOADING PATTERN
The lazy Loading is a design pattern that delays the loading of an object until the time that the object is called upon. They don’t utilize with objects with Magento.

10 SERVICE LOCATOR PATTERN
The service locator pattern is a design pattern that allows a user to get a service by encapsulating the process inside an abstraction layer. This is allows the user to retrieve the appropriate or best services without knowing what that service is at runtime.

11 MODULE PATTERN
The module pattern is basically defines that different domains are grouped into separate modules which function independent of each other and can be plugged-in to the main system as deemed appropriate. It is a form of modular programming that emphasizes the grouping of functionality of a program into independent, inter-changeable modules.

12 OBSERVER PATTERN
The observer pattern is set a certain point during an application’s execution. Other components of the application can "hook" into the event listener and execute their code during this point. Magento has its event-driven architecture and it is a result of an implementation of the observer pattern.

THERE ARE SOME ADDITIONAL DESIGN PATTERNS TO USE IN MAGENTO 2

01 SERVICE CONTRACT DESIGN PATTERN
Magento is an extension based system, which allows a third-party modules, which developer to customize and overwrite on the core parts of its framework. These customization may have many several issues, thus to overcome Magento comes up with service contract pattern. It is a set of the interfaces which act as a layers between end users and business layer.

02 OBJECT MANAGER
There is consist of various pattern such as Dependency injection, Singleton, Factory, Abstract Factory, Composite, strategy, CQRS, Decorator and many more. It has a very big role to run, Magento prohibits the direct use of it. Object manager is responsible for the implement singleton, factory and proxy patterns. It is automatically instantiates parameters in class constructors. Before moving future let’s understand about injectable and non-injectable objects.

03 INJECTABLE OBJECTS
They don't have any own identity such as Event Manager, Customer Account Management Service.

04 NON- INJECTABLE OBJECTS
They entities usually have their identities and state, since they have their identities, such as customer, product etc.

05 DEPENDENCY INJECTION
The dependency injection is an alternative to Mage in Magento 1. It is a concepts of injecting the dependent object through external environment rather than creating them internally. Thus we will be using with resource when our object is being created instead of creating resource when needed. This helps in future modification or customization and testing becomes very easy by mocking required objects.

06 FACTORY PATTERN OR FACTORY CLASSES
The factory classes create a layer between the object manager and business code in Magento 2. It is a classes need not define explicitly as they are auto-generated.

07 PROXY PATTERN
The proxy classes are used to work in place of another class and in Magento 2, they are sometimes used in place of resource needed classes. As per Magento uses constructor injection for object creation and when we instantiate an object all the classes. The constructor will also instantiate thus leading to a chaining of instantiation of the constructor. It can really slow down the process and impact on the performance of an application, however to stop chain instantiation Magento uses proxy classes.

Mysql create user and privileges assign.

 Remove privileges

REVOKE ALL PRIVILEGES ON aws_oxtoolstaging.* FROM 'aws_oxtoolstaging'@'localhost';



Assign privileges

CREATE USER 'username'@'localhost' IDENTIFIED BY 'S1@gingin';

GRANT ALL PRIVILEGES ON databases . * TO 'username'@'localhost';


show privileges

show grants for 'username'@'localhost';

magento 2 how to disable inventory modules.

please look below link it would help. to disable MSI module

 https://meetanshi.com/blog/disable-magento-msi/

Product saleable qty not showing and update in magento 2.x

 I have same issue with resolved, take inventory_source_item table backup first.

Run sql query:

INSERT IGNORE INTO `inventory_source_item` (source_code, sku, quantity, status)
select 'default', sku, qty, stock_status from (`cataloginventory_stock_status` as `lg` join `catalog_product_entity` as `prd` on((`lg`.`product_id` = `prd`.`entity_id`)))

Apply command:

php bin/magento indexer:reindex

php bin/magento cache:flush

How To Install PHP 7.4 / 7.3 / 7.2 on Ubuntu 18.04 / Ubuntu 16.04

 PHP is an open-source programming language widely used for web development. It is an HTML-embedded scripting language for building dynamic web sites.

By default, Ubuntu 18.04 and Ubuntu 16.04 ships PHP v7.2 (EOL) and PHP v7.0 (EOL), respectively.

In this post, we will see how to install PHP 7.4 / 7.3 / 7.2 on Ubuntu 18.04 / Ubuntu 16.04.

Add PHP Repository

OndÅ™ej Surý, a third-party repository, hosted on the launchpad, which offers PHP 7.4 / 7.3 / 7.2 for Ubuntu operating system.

Update the repository cache.

sudo apt update

Install the below packages.

sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https

Add the repository to your system.

sudo apt-add-repository ppa:ondrej/php

Update the repository index.

sudo apt update

Install PHP

Install PHP 7.4 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.4 with the below command.

sudo apt install -y php7.4 php7.4-cli php7.4-common

Install PHP 7.3 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.3 with the below command.

sudo apt install -y php7.3 php7.3-cli php7.3-common

Install PHP 7.2 on Ubuntu 18.04 / Ubuntu 16.04

Install PHP 7.2 with the below command.

sudo apt install -y php7.2 php7.2-cli php7.2-common
You can also have multiple PHP versions on your system. Each PHP version will have separate configuration files under /etc/php/7.X directory.

Verify PHP Version

Once you have installed the PHP package, check its version.

php -v

Output:

PHP 7.4.5 (cli) (built: Apr 19 2020 07:36:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

PHP Support for Web Server

Both Apache and Nginx do not support the processing of PHP files by default when the browser requests the PHP page. So, we need to install the PHP package on the server to support PHP files.

PHP Support for Apache

You can install a below package with Apache webserver to support PHP for the LAMP stack. This package provides the PHP modules for Apache 2 web server.

ADVERTISEMENT

READ: How To Install LAMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y apache2 libapache2-mod-php7.4

PHP Support for Ngnix

Nginx does not have a PHP modules package to support PHP for the LEMP stack. To have PHP support for Nginx, we can use the PHP FastCGI Process Manager to handle PHP requests.

READ: How To Install LEMP Stack on Ubuntu 18.04 / Ubuntu 16.04

Change PHP version, if required

sudo apt install -y php7.4-fpm

PHP-FPM listens on the socket run/php/php7.4-fpm.sock by default.

To make PHP-FPM use TCP connection, edit the below file.

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Then, change the listen parameter.

FROM:

listen = /run/php/php7.4-fpm.sock

TO:

listen = 127.0.0.1:9000

Add socket details in Nginx virtual host.

server {

# other codes

  location ~* \.php$ {
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }
}

Install PHP Extensions

PHP extensions enable specific functions/support for your code. For example, installing PHP MySQL extension will let your PHP code to connect with the MySQL database.

PHP extensions package is normally named like php<version>-<extn_name>.

To install MySQL support for PHP v7.4, you can install a php7.4-mysql package.

sudo apt install -y php7.4-mysql

Once you have installed a required extension, use the below command to verify it.

php -m | grep -i mysql

Output:

mysqli
mysqlnd
pdo_mysql

PHP Extensions for CMS

PHP Extensions for WordPress

The following extensions are required to install and run WordPress on Ubuntu. WordPress recommends having PHP v7.3 for the installation.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-simplexml php7.3-ssh2 php7.3-xml php7.3-xmlreader php7.3-curl  php7.3-exif  php7.3-ftp php7.3-gd  php7.3-iconv php7.3-imagick php7.3-json  php7.3-mbstring php7.3-posix php7.3-sockets php7.3-tokenizer

PHP Extensions for Joomla

The following extensions are required to install and run Joomla on Ubuntu. Joomla requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-xml php-pear php7.3-json

PHP Extensions for Drupal

The following extensions are required to install and run Drupal on Ubuntu. Drupal requires PHP v7.1 and above.

sudo apt install -y php7.3-mysql php7.3-dom php7.3-gd php7.3-json php7.3-pdo php7.3-simplexml php7.3-tokenizer php7.3-xml

Magento 2 Java Script Bundling

JavaScript Bundling


Java-Script bundling is an optimization technique, you can use to reduce the number of files server requests for JavaScript requests. Bundling accomplishes this by merging files multiple JavaScript files together into one file to reduce the number of page requests.

Enable Java-Script bundling

JavaScript bundling does not work unless Magento is in production mode. Once in production mode, JavaScript bundling can only be enabled using the CLI. Follow these steps to setup JavaScript bundling from the CLI.

  1. From the Magento root directory, switch to production mode:

    bin/magento deploy:mode:set production
    
  2. Enable JavaScript bundling:

    bin/magento config:set dev/js/enable_js_bundling 1
    
  3. Optimize bundling by minifying JavaScript files:

    bin/magento config:set dev/js/minify_files 1
    
  4. Enable cache busting on static file URLs. This ensures users get the latest version of the assets anytime they update:

    bin/magento config:set dev/static/sign 1
    
  5. To configure JavaScript bundling, you must disable Javascript file merging. Bundling will not work as the merging of files excludes bundling:

    bin/magento config:set dev/js/merge_files 0
    
  6. Modifying the settings above when Magento is in production mode will require static view files deployment:

    bin/magento setup:static-content:deploy
    
  7. Finally, clear the cache:

    bin/magento cache:clean config
    

    For example, when Sign Static Files is disabled (which is the default: config:set dev/static/sign 0), the URL to a static file might look like this: /static/frontend/Magento/luma/en_US/mage/dataPost.js. But when you enable the setting (config:set dev/static/sign 1), the same URL might look something like this: static/version40s2f9ef/frontend/Magento/luma/en_US/mage/dataPost.js, with a version number added as shown. The next time this file is updated (with bin/magento setup:static-content:deploy), a new version will be generated, causing the browser to download a new file from the server, thus busting the browser’s cache.

How bundling works in Magento

When you enable bundling, Magento combines hundreds of JavaScript files into just a few JavaScript bundles and downloads those bundles for each page. Because the browser downloads the bundles synchronously, page rendering is blocked until all bundles finish downloading. But the time saved from reducing server requests from hundreds to just a few, usually offsets the cost of downloading the bundles synchronously.

Excluding files

The <exclude> node in the etc/view.xml file for a theme specifies the files to exclude from the Magento JavaScript bundling process. JavaScript files excluded from bundling are loaded asynchronously by RequireJS as needed.

As such, you should exclude the JavaScript files you use for testing or development so that they are not loaded on every page.

The following code snippet from Magento’s Luma theme shows the types of files you should exclude from the bundling process.

Show example

Setting bundle file size

The bundle_size variable controls the file size of the generated bundles. Specifying a large bundle_size reduces the number of bundles generated, but generates larger file sizes. Specifying a smaller bundle_size generates more bundles with a smaller file sizes.

Example:

1
2
3
<vars module="Js_Bundle">
    <var name="bundle_size">1MB</var>
</vars>

The goal is to balance the number of bundles to download with the size of each bundle. As a rule of thumb, each bundle should be at least 100 kB.

Fine tuning your theme

There are many ways to tune your theme using the etc/view.xml file.

For example, the Magento Luma theme is configured to work well for all pages, but you can maximize browser performance for home, catalog, or product pages by adding items to or removing items from the <exclude> node.

Follow these steps to help you identify which JavaScript files to bundle for your theme:

  1. Create a blank page with the layouts you would like to tune.
  2. Compare the JavaScript files loaded in the pages with the JavaScript files in Magento.
  3. Use the results of that comparison to build your exclude list.

 

How to check if Magento is running in production mode or developer mode

If you want to know. your Magento 2 website is running on which environment you can run below command on terminal and very easily you can kn...

Popular Posts

Posts