9

We can extend controller in two ways: using preference and using plugin. But I do not understand that, which one is good and advantages/disadvantages of preference vs plugin.

<preference for="Magento\Checkout\Controller\Index\Index" type="mymodule\Helloworld\Controller\Index\Index" />

<type name="Magento\Catalog\Model\Product">
    <plugin name="getname-test-module" type="mymodule\Helloworld\Model\Plugin\Product" sortOrder="10"/>
</type>
11

Use plugins whenever possible, then observers (see the pros and cons of each), this will allow you to avoid conflicts with other extensions (3rd party modules).

Preference should be used only if the whole core class/interface implementation should be substituted with custom one. Take into account that only one preference is active at any time (which one, is defined by sequence defined in module.xml), so if another extension defines preference for the same class/interface, there will be a conflict (these extensions cannot work together).

4
  • If your functionality fully replace the core functionality then you may use preferences.
  • If you want add the functionality to the base or partially replace some class method then please use plugins.
  • 1
    Seems prudent to note that if you replace core functionality then the burden is on you to monitor core updates and ensure your custom modules are up to date – Robbie Averill Dec 16 '15 at 17:47
1

adittional infos: almost all preferences used in intern m2 it's just for keep relation between base concrete class and interface, like:

<preference for="Magento\Store\Model\StoreManagerInterface" type="Magento\Store\Model\StoreManager" />

as you case it's used one time i think:

<preference for="Magento\ImportExport\Controller\Adminhtml\Export\GetFilter" type="Magento\AdvancedPricingImportExport\Controller\Adminhtml\Export\GetFilter" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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