2

What is the difference between Magneto1 and Magento2 controller in term of the development ?

2 Answers 2

4

To add on what Pankaj Pareek said...
Autoloading:
In Magento 2, controllers don't have a special treatment like it did in Magento 1.
In Magento 2, controllers can be found in the Controller folder of the module and they are loaded following the same autoloader pattern. Class name must match folder path name and they don't need the Controller suffix.
Example:
M1: file: Mage/Catalog/controllers/ProductController, class Mage_Catalog_ProductController. Notice that controllers is lowercase and it is not included in the class name.
M2: file Magento/Catalog/Controller/Product/View, class Magento\Catalog\Controller\Product\View.
The upside is that if you want to extend a controller class, you don't need to add require_once 'Controller/File/Path/Here.php'. The autoloader does its job.

Return
In Magento 1, controller actions don't really return anything. They can just render the layout or redirect, or forward to another action.
In Magento 2, you can still do the same as in M1, but it's highly discouraged. Instead your actions should return and instance of Magento\Framework\Controller\ResultInterface

2

Its Completely Changed.

In Magento 1 Controller is one php file that contain all the actions functions.

In Magento 2 Controller work like as folder that group all the Actions PHP files and each Actions PHP file have one execute function as entrypoint.

Check below Contacts Module Index Controller In Magento 1 and Magento 2 for more difference:

Magento 1

https://github.com/engineyard/magento-ce-1.9/blob/master/app/code/core/Mage/Contacts/controllers/IndexController.php

Magento 2

https://github.com/magento/magento2/tree/2.2-develop/app/code/Magento/Contact/Controller/Index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.