Magento 2 Interview Questions
1 What is a Plugin
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.
2 In which directory we write our own custom module in Magento2
If we need to create our own module e.g Hello World then we define it in
app/code folder. It will be app/code/VendorName/moduleName.
e.g : app/code/Letsknowit/Hello_world.
3 From where in admin panel we can enable/disable cache
4 What is the command to enable/disable a module in Magento2
php bin/magento module:enable module_name
php bin/magento module:disable module_name
5 Which method a controller loads by default in Magento2
public function execute()
6 How to enable/disable module in magento2To enable we need to runphp bin/magento module:enable Namespace_modulenamee.g php bin/magento module:enable Letsknowit_Custommoduleto disable we need to runphp bin/magento module:disable Namespace_modulenamee.g php bin/magento module:disable Letsknowit_Custommodule
7 What is the command to know the status of all modules in magento2
php bin/magento module:status
8 What is the command to clear and flush magento cache
To clean the cachephp bin/magento cache:cleanTo flush cachephp bin/magento cache:flush
9 How to override controller in Magento2
To override controller we need to create a basic module structure. In our below code we have Hello module that will override Product view controller.
1. Create di.xml file in Folder Letsknowit/Hello/etc
our complete path is app/code/Letsknowit/Hello/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogControllerProductView" type="LetsknowitHelloControllerRewriteProductView" />
2. Now we need to create view.php controller file in LetsknowitHelloControllerRewriteProduct
* Hello Rewrite Product View Controller
class View extends MagentoCatalogControllerProductView
* @return MagentoFrameworkControllerResultRedirect|MagentoFrameworkViewResultPage
public function execute()
// Do your stuff here
In the same way we can override other controllers
10 What is a UI component in Magento2
In Magento UI component is a combination of:
1. XML declaration that specifies the component’s configuration settings and inner structure.
3. Related template(s)
11 What is Object Manager in Magento2
Object manager is a class that intiates the object before begining of Bootstrapping process.
12 What is the command to enable developer/production mode.
To see on which mode it is currently on :
To enable developer mode
bin/magento deploy:mode:set developer
To enable production mode
bin/magento deploy:mode:set production
13 What are the files needed to bootstrap a module
Mainly there are three files for a module to bootstrap.
and composer. json
14 What are Magento area types.
An area in Magento is a logical component which can help you organizes code in order to optimize the processing of the request.
There are 6 magento area types
1. Magento Admin: adminhtml
2. Storefront: frontend
3. Basic: base
4. Web API REST: webapi_ rest
5. Web API SOAP: webapi_soap
6. Cron: crontab
15 What are the limitations of using plugins for customization?
• Plugins only work on public functions (not protected or private).
• Plugins do not work on final classes or final methods.
• Plugins do not work on static methods.
16 In which cases should plugins be avoided?
Plugins are useful to modify the input, output, or execution of an existing method.
Plugins are also best to be avoided in situations where an event observer will work. Events work well when the flow of data does not have to be modified.
17 How do we enable/disable maintenance mode in Magento?
To enable/disable maintenance mode in Magento we need to run below commands respectively
18 What is the difference between sales rules and catalog rules?
Sales rules apply to products that are already added in the cart. Catalog rules apply to products before added to the cart.
19 What are Extension Attributes.
Extension attributes are new in Magento. It used to adding additional data to an existing entity, especially if that data came from another table or was calculated was difficult and error-prone.
At this point, extension attributes exert little control over how you implement, which gives you great power.
20 How to turns on logging of database queries
To turn on the logging of Db queries we need to run the below command
21 What is difference between cache clean and cache flush
php bin/magento cache:clean
Cache cleans deletes all items from enabled Magento cache types only.
In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses.
php bin/magento cache:flush
Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage.
22 What is difference between ObjectManager::create($className) and ObjectManager::get($className)
ObjectManager::create($className) : create always returns a new instance of the requested class.
ObjectManager::get($className) : get returns an instance which
may be one that has already been instantiated
23 What is Proxy Design pattern in Magento 2
In magento2 a new concept is introduced of dependency injection. There are many types of dependency injection but magento2 uses mainly constructor injection and method injection.
Constructor injection says if you want to use any other class object in your class, don’t instantiate it inside the class, inject the object into the constructor
and then use the object in the class, so when your class will be instantiated all the dependencies injected in your class constructor will also get instantiated, and it will trigger a chain reaction of object creation, this can really slow down the process, so to stop the chain reaction of object creation magento 2 uses proxy design pattern.
It's a new feature in Magento. It is a set of classes that are automatically generated by Magento during compilation.
Proxies are used to lazy load a class they extend. So Injecting a Proxy object is light and takes up little resources. The object is instantiated only when it’s time for the code to use one of the methods.