0

can anyone tell me what am I doing wrong with this code

<?php
namespace Feedback\Addon\Controller\Index;
use Magento\Framework\App\Action\Context;
use Feedback\Addon\Model\InsertFactory;
class Save extends \Magento\Framework\App\Action\Action
{
 /**
 * @var Test
 */
 protected $_test;
 protected $_storeManager;
 public function __construct(
 Context $context,
 array $data = [],
 \Magento\Store\Model\StoreManagerInterface $storeManager, 
 InsertFactory $test
 ) {
 $this->_storeManager = $storeManager;
 $this->_test = $test;
 parent::__construct($context);
 }
 public function execute()
 {
 $post = $this->getRequest()->getPostValue();
 $test = $this->_test->create();
 $data=array(
 'first_name'=> $post['first_name'],
 'last_name'=> $post['last_name'],
 'email' => $post['email'],
 'mobile_no' => $post['mobile_no'],
 'feedback_content' => $post['feedback_content']
 ); 
 $test->setData($data);
 if($test->save())
 {
 $this->messageManager->addSuccessMessage(__('You saved the data.'));
 $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
 $templateVars = array(
 'store' => $this->storeManager->getStore(),
 'customer_name' => 'John Doe',
 'message' => 'Hello!!.'
 );
 $from = array('email' => "[email protected]", 'name' => 'Name of Sender');
 $this->inlineTranslation->suspend();
 $to = array('[email protected]');
 $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
 $templateId = $this->scopeConfig->getValue ( 'feedback/custom/email_template', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId );
 $transport = $this->_transportBuilder->setTemplateIdentifier($templateId, $storeScope)->setTemplateOptions($templateOptions)
 ->setTemplateVars($templateVars)
 ->setFrom($from)
 ->addTo($to)
 ->getTransport();
 $transport->sendMessage();
 $this->messageManager->addSuccess(__('Mail Sent Successfully'));
 $this->inlineTranslation->resume();
 }
 else
 {
 $this->messageManager->addErrorMessage(__('Data was not saved.'));
 }
 $resultRedirect = $this->resultRedirectFactory->create();
 $resultRedirect->setPath('*/*/');
 return $resultRedirect;
 }
}

I am trying to sending mail to the user after saving data but this is generated this error

Fatal error: Uncaught TypeError: Argument 3 passed to Feedback\Addon\Controller\Index\Save\Interceptor::__construct() must be of the type array, null given, called in /var/www/html/nuluv/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111 and defined in /var/www/html/nuluv/generated/code/Feedback/Addon/Controller/Index/Save/Interceptor.php:11 Stack trace: #0 /var/www/html/nuluv/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(111): Feedback\Addon\Controller\Index\Save\Interceptor->__construct(Object(Magento\Framework\App\Action\Context), Object(Magento\Store\Model\StoreManager), NULL, Object(Feedback\Addon\Model\InsertFactory)) #1 /var/www/html/nuluv/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Feedback\Addon\...', Array) #2 /var/www/html/nuluv/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Compiled->create('Feedback\Addon\...', in /var/www/html/nuluv/generated/code/Feedback/Addon/Controller/Index/Save/Interceptor.php on line 11

Manashvi Birla
8,8739 gold badges29 silver badges53 bronze badges
asked Jan 16, 2019 at 9:19
1
  • Try to run following command & then check : php bin/magento s:up && php bin/magento s:di:c Commented Jan 16, 2019 at 9:22

1 Answer 1

1

make your constructor receive the mandatory parameters before the optional ones:

public function __construct(
 Context $context,
 \Magento\Store\Model\StoreManagerInterface $storeManager, 
 InsertFactory $test,
 array $data = []
) {
 $this->_storeManager = $storeManager;
 $this->_test = $test;
 parent::__construct($context, $data);
}

and clear the contents of folder generated/code.

answered Jan 16, 2019 at 9:43
2
  • hi thanks for the help! I tired your code buy its generated error - Extra parameters passed to parent construct: $data. File: /var/www/html/nuluv/app/code/Feedback/Addon/Controller/Index/Save.php Total Errors Count: 1 Commented Jan 16, 2019 at 9:53
  • then remove $data from parent::__construct($context, $data); Commented Jan 16, 2019 at 12:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.