cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3 override controller and extend parent constructor

SOLVED

Magento 2.3 override controller and extend parent constructor

Hello,

 

I want to override a controller which has a big constructor, which looks like this:

 

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
) {
$this->helper = $helper;
$this->customerRepository = $customerRepository;
$this->inlineTranslation = $inlineTranslation;
$this->transportBuilder = $transportBuilder;
$this->cookieMetadataManager = $cookieMetadataManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->customerSession = $customerSession;
$this->scopeConfig = $scopeConfig;
$this->accountManagement = $accountManagement;
$this->urlFactory = $urlFactory;
$this->formFactory = $formFactory;
$this->subscriberFactory = $subscriberFactory;
$this->regionDataFactory = $regionDataFactory;
$this->addressDataFactory = $addressDataFactory;
$this->customerUrl = $customerUrl;
$this->escaper = $escaper;
$this->customerExtractor = $customerExtractor;
$this->dataObjectHelper = $dataObjectHelper;
$this->formKeyValidator = $formKeyValidator;
$this->logger = $logger;
$this->accountRedirect = $accountRedirect;
$this->addressHelper = $addressHelper;
$this->storeManager = $storeManager;
parent::__construct($context);
}
I overwritten this controller without constructor (it takes the parent one in this case), but I also want to load a model in that overwritten controller to save data in a custom table. Basically, I want to extend the parent constructor and also load other models in the extended file.
 
How can I extend the constructor so that I can also load the model inside it?
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.3 override controller and extend parent constructor

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.

View solution in original post

6 REPLIES 6

Re: Magento 2.3 override controller and extend parent constructor

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.

-----
If Issue Solved, Click Kudos and Accept As solutions.
Sohel Rana, 7x Magento 2, 2x Magento 1 Certified

Re: Magento 2.3 override controller and extend parent constructor

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:

protected $addressRepository;

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,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
$this->addressRepository = $addressRepository;
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
);
}
 
...
 
$address = $this->addressRepository->getById($billingAddress);
$address->setTelephone($phone);

try{
   $this->addressRepository->save($address);
}catch (\Exception $e) {
   die("Error message: ".$e->getMessage());
}
 
What's the problem? 

Re: Magento 2.3 override controller and extend parent constructor

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. 

 

Re: Magento 2.3 override controller and extend parent constructor

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.

Re: Magento 2.3 override controller and extend parent constructor

Hi @Pronko,

 

I used a plugin. 

 

Thanks. 

Re: Magento 2.3 override controller and extend parent constructor

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/