0

I have to override 2 controller 1 override is working and 1 override not working

<?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\Customer\Controller\Account\ForgotPassword" type="\Copper\Customer\Controller\Auth\ForgotPassword" />
    <preference for="\Magento\Customer\Controller\Account\ForgotPasswordPost" type="\Copper\Customer\Controller\Auth\ForgotPasswordPost" />
</config>

While \Magento\Customer\Controller\Account\ForgotPassword override is working just fine. \Magento\Customer\Controller\Account\ForgotPasswordPost is not overriding

Do anyone know Why ?

Controller ForgotPasswordPost.php

<?php
namespace Copper\Customer\Controller\Auth;

class ForgotPasswordPost extends \Magento\Customer\Controller\Account\ForgotPasswordPost
{
    public function execute()
    {
        echo "hello";die();
    }
}
12
  • you should check if any other module/extension is overriding the same controller action
    – Piyush
    Jan 31 '18 at 13:51
  • there is no third party module installed Jan 31 '18 at 13:52
  • can you add your controller code here?
    – Piyush
    Jan 31 '18 at 13:54
  • @Piyush updated question Jan 31 '18 at 13:56
  • Your code seems right , check the file name and path
    – Piyush
    Jan 31 '18 at 14:02
2

Please remove \ form starting it will be worked

\Magento\Customer\Controller\Account\ForgotPassword it should be like Magento\Customer\Controller\Account\ForgotPassword

Please try this di.xml

<?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\Customer\Controller\Account\ForgotPassword" type="Copper\Customer\Controller\Auth\ForgotPassword" />
    <preference for="Magento\Customer\Controller\Account\ForgotPasswordPost" type="Copper\Customer\Controller\Auth\ForgotPasswordPost" />
</config>
7
  • tried with and without \ only Magento\Customer\Controller\Account\ForgotPassword override is working. Magento\Customer\Controller\Account\ForgotPasswordPost is not overriding Feb 1 '18 at 5:29
  • Apparently everything was fine, the issue was Captcha was validated via observer and if incorrect/empty it was redirect from there only and I was trying to hit URL directly, and I was redirection back from observer only. did not even reaching controller. Feb 1 '18 at 7:33
  • ok so all working fine now? Feb 1 '18 at 13:20
  • yes, apparently it was working from the starting. its just the captcha observer is redirecting before reaching the controller method. Feb 2 '18 at 7:18
  • 1
    Thanks @NavinBhudiya it is also working for me! Sep 4 '20 at 5:44
1

It's should working fine. There is the only one thing you may have not took into account is put di.xml file in correct area. So di.xml file should be located in app\code\Copper\Customer\etc

And another one thing. If you just want to override method or add new functional before/after to method, please use plugin system it is better way then preference.

http://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

3
  • can you give hint how to use plugin for my situation, I have to redirect to example.com/password-change-success page instead of login page after successful password change. Feb 1 '18 at 7:01
  • I cannot use beforeExecute too early, don't know if aroundExecute is applicable and how can i deduct if password change was successful in afterExecute ? Feb 1 '18 at 7:03
  • Apparently everything was fine, the issue was Captcha was validated via observer and if incorrect/empty it was redirect from there only and I was trying to hit URL directly, and I was redirection back from observer only. did not even reaching controller., but I would like to know how to do this with plugin Feb 1 '18 at 7:33

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.