cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3 override controller and extend parent constructor

SOLVED
Highlighted

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
Highlighted

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

5 REPLIES 5
Highlighted

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
Highlighted

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? 
Highlighted

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. 

 

Highlighted

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

Highlighted

Re: Magento 2.3 override controller and extend parent constructor

Hi @Pronko,

 

I used a plugin. 

 

Thanks.