magento2: Controller preference not working after setup:di:compile

Preconditions

Magento 2.1.x

Steps to reproduce

  1. Installed Magento v2.0.7 (no tested in previous versions).
  2. Create a custom extension.
  3. Use preference to override controllers.
  4. Run magento setup:di:compile (executed successfully)

Expected result

  1. My controller action should rewrite original Magento action.

Actual result

  1. Original Magento Action is executed.

Making it more clear, here is the piece of code which rewrites the Magento controllers (under etc/di.xml, tested also under etc/adminhtml/di.xml, with the same result):

<?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" />

</config>

If I don’t run magento setup:di:compile, preferences will work without any problem. But, after run it, preferences stop working (tested with frontend and admin controllers)

I have checked var/di/adminhtml.ser file and interceptors for my classes seem to be created correctly (not sure if such information can help)

I hope the information is enough detailed, thank you in advance!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 14
  • Comments: 19 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I have been looking into this issue today, as it’s blocking a release for us.

The problem appears to be if there is a plugin declared on the class: <plugin type="...."></plugin>

This creates an interceptor file, which then means any further preference is completely ignored during di compilation.

The same issue; in DEVELOPER MODE my controller preferences for adminhtml was neglected by di.xml, so they stopped working. After a successful setup:di:compile, the interceptors in var/generation were created successfully, but those are never executed in runtime.

Here is a workaround that worked for me;

  • remove var/di

everything is up an running again!

I think this is not a bug, but a confusion around development modes and how var/di is utilized… Seems like we SHOULD remove ‘var/di’ every time after compiling dependencies on DEVELOPER MODE.

Check this article https://www.cadence-labs.com/2017/07/magento-2-run-setupdicompile/

Hi, I have the same issue, I tried to extend the checkout controller using preference. My code is shown below: <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Checkout\Controller\Cart\Add" type="SCI\Checkout\Controller\Cart\Add" /> </config> It is working fine and perfect, but after we deploy to production mode the preference is not executed. I double check by editing the vendor codes directly and so it works. So therefore it does not call my preference anymore. Any suggestions? Thank you.