I need to rewrite a couple of adminhtml controllers in Magento 2. I am using preference and my configuration in etc/di.xml file looks like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Widget\Controller\Adminhtml\Widget\LoadOptions"
                type="CustomModule\CustomWidgets\Controller\Widget\Adminhtml\Widget\WidgetLoadOptions" />

    <preference for="Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\OnInsert"
                type="CustomModule\CustomWidgets\Controller\Cms\Adminhtml\Wysiwyg\Images\WidgetOnInsert" />


After clear all generated folders (var/cache, var/di, var/generation) it works OK. The problem starts once I run magento setup:di:compile command. Compilation is executed successfully (tested in developer and production modes) but preference doesn't work (original Magento method is executed instead of mine).

Can someone help me? thanks in advance!


If someone has same issue, I have reported some time ago the issue in Magento 2 repository and they confirmed there was a bug:


I will update the answer once they get it solved.

1 Answer 1


I may be wrong but I'm pretty sure it's because those controllers are admin controllers and thus the corresponding di.xml should not be global.

I reckon you should move your di.xml from etc to etc/adminhtml

  • Hi Raphael, thanks for answering, I have tested your suggestion but it doesn't work, also I have tested with some frontend controller and I have the same issue. Did you try to use preference + compilation with some controller and it worked? I tested in a clear Magento 2 installation and have the issue also
    – joc_hhop
    Jun 22, 2016 at 9:53
  • @joc_hhop pretty sure preference can work with almost any class Jun 22, 2016 at 10:14

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.