Hello,
I want to override a controller which has a big constructor, which looks like this:
Solved! Go to Solution.
Hi @Anonymous
It is better to use Plugin for adding a custom logic or alternatively an observer.
Let me know if you haven't fixed the issue so I will advice.
Try following way:
public function __construct( Context $context, Session $customerSession, ScopeConfigInterface $scopeConfig, AccountManagementInterface $accountManagement, UrlFactory $urlFactory, FormFactory $formFactory, SubscriberFactory $subscriberFactory, RegionInterfaceFactory $regionDataFactory, AddressInterfaceFactory $addressDataFactory, CustomerUrl $customerUrl, Escaper $escaper, CustomerExtractor $customerExtractor, DataObjectHelper $dataObjectHelper, Validator $formKeyValidator, Data $helper, CustomerRepositoryInterface $customerRepository, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, PhpCookieManager $cookieMetadataManager, CookieMetadataFactory $cookieMetadataFactory, LoggerInterface $logger, AccountRedirect $accountRedirect, Address $addressHelper, StoreManagerInterface $storeManager, \YourVendor\YourModule\Model\YourModel $yourModel ) { $this->yourModel = $yourModel; parent::__construct( $context, $customerSession, $scopeConfig, $accountManagement, $urlFactory, $formFactory, $subscriberFactory, $regionDataFactory, $addressDataFactory, $customerUrl, $escaper, $customerExtractor, $dataObjectHelper, $formKeyValidator, $helper, $customerRepository, $inlineTranslation, $transportBuilder, $cookieMetadataManager, $cookieMetadataFactory, $logger, $accountRedirect, $addressHelper, $storeManager ); }
After your new change removes generated/* directory.
Hi Sohel,
I tried your solution, no error given, but it's not working.
What I want is to update the address phone number. This is how my code looks:
It's working. The thing was that I didn't had access to the addressID in that moment because the account wasn't created.
I overwritten the execute method, added a new function which is called after all the other things are done and before redirect and in that function I did the address update.
Overriding a controller is same like overriding any class in Magento 2. You will need to add 3 files to add/modify for your custom plugin.
module.xml, di.xml and the controller class.
module.xml
This is the part where most development forget about. In this you have to all the sequence in module.xml file. Modifying the core functionality won’t create any harm. It will work without module.xml but if you are overriding a class which is not a core but a custom plugin or third party plugin then it will create an issue.
Check here with an example they have given to do same https://learningmagento.com/overriding-controller-class-in-magento-2/