Magento2 Interview Questions and Answer

1) Question : Technology stacks of magento 2?

Answer :- Apache, Nginx, PHP, MySQL, Varnish, Redis, HTML5, CSS3, RequireJS/Knockout.js, Zend Framework


2) Question : Magento 2 architecture layers

Answer :-

  • Presentation Layer = It contains all controllers and View elements such as - layouts, templates, block, and css.js, etc.
  • Service Layer = It provides a bridge between the presentation and domain layer and resource-specific data.
  • Domain Layer = The domain layer is responsible for business logic. It does not contain database-specific or resource-specific information.
  • Persistence Layer = It describes the resource model, which is responsible for data extraction and modification in the database using the CRUD requests.


3) Question : How is Magento 2 better than Magento 1?

Answer :- Improved architecture with the latest versions of PHP, Nginx, Apache, Zend, Symphony. It is faster than Magento 1, and gives better performance The dashboard and admin panel is cleaner and more user-friendly. More advanced features like page builder and PWA integration. Easier to install and update extensions Better backend UI


4) Question : MVC (Model View Controller)

Answer :- Model = inetract with databased View = template files togenerate the html output Controller = defines the main actions logic


5) Question : MVVM (Model View ViewMode)

Answer :- Magento 2 is a Model View ViewModel (MVVM) system. While being closely related to its sibling Model View Controller (MVC), an MVVM architecture provides a more robust separation between the Model and the View layers. https://www.toptal.com/magento/magento-2-tutorial-building-a-complete-module https://www.yireo.com/blog/2017-08-12-viewmodels-in-magento-2


6) Question :- what is Object manager?

Answer :- The ObjectManager is a Magento service class that instantiates objects at the beginning of the bootstrapping process. Magento uses class constructor signatures to retrieve information about an object’s constructor dependencies. When a class is constructed, the object manager injects the class’s dependencies, defined in the di.xml file, into the class constructor. Since the object manager provides its service indirectly, your class should not depend on the ObjectManager object itself. The only exceptions are custom factories with complex logic and integration tests that need environment setup.


7) Question : What is dependecy injection?

Answer :-Dependency Injection is a design pattern that allows an object A to declare its dependencies to an external object B that supplies those dependencies. The dependencies declared by A are usually class interfaces and the dependencies B provides are concrete implementations for those interfaces. This allows for loose coupling of code because object A no longer needs to be concerned with initializing its own dependencies. Object B decides which implementations to provide to object A based on a configuration or desired behavior. Magento 2 Dependency Injection includes two types: Constructor Injection and Method Injection.


8) Question : What is constructor Injection?

Answer :- Constructor Injection is one of the commonly used ways to inject dependencies in Magento 2. In this type of DI, all you need to do is add a parameter in the class constructor to inject the dependency. It should be the first choice because Constructor Injection addresses the most simple scenario where a class requires one or more dependencies.


9) Questions : Method Injection

Answer :-Method injection is another type of ID used in Magento 2, which passes the dependency as a method parameter to use it in the class. Method injection can be used best when the dependency may vary on each method call. When an object requires performing specific actions on a dependency that cannot be injected, it is recommended to use the Method Injection.


10) Question :- What is Factory object?

Answer:- Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist.

we use $topic = $this->_topicFactory->create(); to create the model object.


11) Question:- What Is Proxy/Proxies?

Answer:- Proxy classes are used to work in place of another class and in Magento 2 they are sometimes used in place of resource hungry classes. To understand what proxy classes do let’s see the reason which leads to the occurrence of proxy classes. As we know Magento uses constructor injection for object creation and when we instantiate an object all the classes in its constructor will also instantiate thus leading to a chain of instantiation via a constructor, this can really slow down the process and impact the performance of an application, so to stop chain instantiation Magento uses proxy classes.

Some common convention Magento follow while creation of proxy:-

  • Namespace of proxy class will be same as original (Magento\Catalog\Model\Product\Attribute\Source\Status)
  • Proxy class only extends one object i.e, object manager Has magic functions such as __sleep, __wake which are invoked only on certain action and function such as __clone will make an object of original class and will provide the object only when it is needed (making use of lazy loading design pattern), thus improving the performance of application

ps://www.mageplaza.com/devdocs/proxies-magento-2.html


12) Question:- Overriding Block, Model, Controller in Magento2

Answer:- i)Create di.xml

ii) Add preference tag with "for" and "type" Parameter


13) Question:- what is virtualType?

Answer:-Virtual types are a way to inject different dependencies into existing classes without affecting other classes.

For example, the Magento\Framework\Session\Storage class takes a $namespace argument in its constructor, which defaults to the value 'default', and you could use the type definition to change the namespace to 'core'.

core

The above config would make it so that all instances of Magento\Framework\Session\Storage have a namespace of 'core'. Using a virtual type allows for the equivalent of a sub-class to be created, where only the sub-class has the altered argument values.

In the codebase we see the following two configurations:

core
Magento\Core\Model\Session\Storage

The first snippet creates a virtual type for Magento\Core\Model\Session\Storage which alters the namespace, and the second inject the virtual type into Magento\Framework\Session\Generic. This allows Magento\Framework\Session\Generic to be customized without affecting other classes that also declare a dependency on Magento\Framework\Session\Storage


14) Questions:- what is Observers?

Answer:- Observers are a certain type of Magento class that can influence general behavior, performance, or change business logic. Observers are executed whenever the event they are configured to watch is dispatched by the event manager.

Creating an observer To create an observer, you must place your class file under your /Observer directory. Your observer class should implement Magento\Framework\Event\ObserverInterface and define its execute function.


15) Question :- what is Plugins?

Answer:-A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface.

Extensions that wish to intercept and change the behavior of a public method can create a Plugin class.

Limitations

Plugins can not be used on following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

16) Question:- If we want to implment same functionality and that can be done by plugin and event so which option we can use?

Answer:- If we can use plugin and event for functionality so we should use the event because if we use plugin and in that we get any error so functionality is not working. But in event functionality is working if we get any error.

Example

If we want to do something with order so in that we used plugin and we get error in plugin funtion so order process is not working.But if we used observer and we get the error so order is placed.


17) Question:- What is repository ?

Answer:- Repositories give service requestors the ability to perform create, read, update, and delete (CRUD) operations on entities or a list of entities. A repository is an example of a service contract, and its implementation is part of the domain layer.


18) Question:- Injectable and Non-Injectable Objects in Magneto 2

Answer:- Newable, or non-injectable, objects are objects that cannot be injected. They are obtained by creating a new class instance every time they are needed. For example, this could be orders, cart items, users and so on so forth. If you want to use non-injectable items in your code you must request their factory. An example of this could be trying to load numerous products. Your code then will be subject to product factory and with that object you would use the ObjectManager method for the identity of the products that you are trying to load.

Injectable objects are singleton service objects obtained through dependency injection. The object manager uses the configuration in the di.xml file to create these objects and inject them into constructors.Injectable objects can depend on other injectable objects in their constructor as long as the dependency chain does not circle back to the original injectable object.

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/depend-inj.html


19) Question:- What Is Service Contract In Magrento2 ?

Answer:- In magento2 service contract is a set of classes and interfaces that provide some services that hides the business logic and also provides data integrity, so you can create modules using magento2 service contract layer without worrying about the magento2 upgrades(until any major ones). https://www.interactivated.me/blog/service-contracts-magento-2/

There are three service interfaces subtypes:

  • Repository Interfaces
  • Management Interfaces
  • Metadata Interfaces

20) Question:- what is ViewModel?

Answer:-  View models can be used by passing the view model class as an argument to a template’s block in the page layout configuration file. In the following example snippet, MyNewViewModel is the view model class of the OrangeCompany_Catalog module passed as an argument to a block.

In the following example, the same view model is used with an existing block in Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml.

OrangeCompany\Catalog\ViewModel\MyNewViewModel

The view model class must always implement the interface \Magento\Framework\View\Element\Block\ArgumentInterface. For example:

namespace OrangeCompany\Catalog\ViewModel; class MyNewViewModel implements \Magento\Framework\View\Element\Block\ArgumentInterface { public function getTitle() { return 'Hello World'; } }


21) Question :- what is extension attribute?

Answer:- Extension attributes on the other hand are generally used for more complex data types such as adding additional complex data into an entity from a custom external table.

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html


22) Question:- what is Custom attributes?

Answer:- Custom attributes are those added on behalf of a merchant. For example, a merchant might need to add custom attributes to product entity named ‘customizeable’ in which he want to save information that this product will be customized from 3rd party or not .

The attributes added to describe an entity, such as product attributes, customer attributes etc. These are a subset of EAV attributes.


23) Question:- what is Graph QL?

Answer:-

  • GraphQL is a query language for using APIs.
  • get smaller amounts of data and to make fewer API requests.
  • Magento is writing an entirely new layer that interfaces directly to the Query API.
  • https://www.rakeshjesadiya.com/graphql-in-magento-2/


24) Question:- When is code generated?

Answer:- Provided the Magento application is not set for production mode, code is generated when the Magento application cannot find a class when executing code.


25) Question :- What is DocBlock?

Answer:- 

  • This standard defines By Magento
  • documentation above function so every one know the function use and parameters


26) Question :- Composer.json

Answer:-Mainly Magento 2 use the Composer to management of modules and the core library which are used in magento 2 like Zend,Symfony,sebastian,phpunit.

Composer is a dependency manager for magento 2.

Composer declare the libraries of your project which are depends on your projects So its useful in your project if you have so many libraries or modules.

you can check composer.json file in root of your project.


27) Question :- what isSequnce tag?

Answer :- declares the list of components that must be loaded before the current component is loaded. It’s used for loading a different kind of files: configuration files, view files (including CSS, LESS, and template files), or setup classes.

Note that does not affect the loading of regular classes (non-setup classes). Setup classes are classes in the component that create or update database schema or data.


28) Question :- what is Profiler in magento 2?

Answer :- You can use a built-in profiler with Magento to perform tasks such as analyzing performance. The nature of profiling depends on the analytical tools you use. We support multiple formats, including HTML. When you enable the profiler, a var/profiler.flag file generates indicating the profiler is enabled and configurations. When disabled, this file is deleted.

php bin/magento dev:profiler:enable html

php bin/magento dev:profiler:enable csvfile

php bin/magento dev:profiler:disable


29) Question :- What Is The Difference Between Cache:clean And Cache:flush?

Answer :- Typically, cache:clean deletes all enabled cache related to magento whereas cache:flush deletes the whole cache storage, whether its magento cache or any third party cache (whether enabled or disabled)


30) Question :- what is Developer mode?

Answer :-

  • Static file materialization is not enabled.
  • Uncaught exceptions displayed in the browser
  • Exceptions thrown in error handler, not logged
  • System logging in var/report, highly detailed.

You should use the Developer mode while you are developing customizations or extensions. The main benefit to this mode is that error messages are visible to you. It should not be used in production because of its impact on performance. In Developer mode, static view files are generated every time they are requested. They are written to the pub/static directory, but this cache is not used. This has big performance impact, but any changes a developer makes to view files are immediately visible.

Uncaught exceptions are displayed in the browser, rather than being logged. An exception is thrown whenever an event subscriber cannot be invoked.

System logging in var/report is highly detailed in this mode.


31) Question :- What is Production mode?

Answer :- Deployment phase on the production system; highest performance
Exceptions are not displayed to the user -- written to logs only.
This mode disables static file materialization.

The Magento docroot can have read-only permissions.

You should run Magento in Production mode once it is deployed to a production server.

Production mode provides the highest performance in Magento 2.

The most important aspect of this mode is that errors are logged to the file system and are never displayed to the user. In this mode, static view files are not created on the fly when they are requested; instead, they have to be deployed to the pub/static directory using the command-line tool. The generated pages will contain direct links to the deployed page resources.

Any changes to view files require running the deploy tool again.

Because the view files are deployed using the CLI tool, the web user does to need to have write access. The Magento pub/static directory can have read-only permissions, which is a more secure setup on a publicly accessible server.


32) Question :- what is Default mode?

Answer :-

  • Used when no other mode is specified
  • Hides exceptions from the user and writes them to log files
  • Static file materialization is enabled.
  • Not recommended / not optimized for production: caching impacts performance negatively.

As its name implies, Default mode is how the Magento software operates if not other mode is specified.

In this mode, errros are logged to files in var/reports and are never shown to a user. Static view files are materialized on the fly and then cached.

In contrast to the developer mode, view file changes are not visible until the generated static view files are cleared.

Default mode is not optimized for a production environment, primarily because of the adverse performance impact of static files being materialized on the fly rather than generating and deploying them beforehand.

In other words, creating static files on the fly and caching them has a greater performance impact than generating them using the static file creation command line tool.

No comments:

Post a Comment