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' => "test@example.com", 'name' => 'Name of Sender');
            $this->inlineTranslation->suspend();
            $to = array('abc@example.com');

            $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

  • Try to run following command & then check : php bin/magento s:up && php bin/magento s:di:c – Nauman m. Mansuri Jan 16 at 9:22
0

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.

  • 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 – Akash Agrawal Jan 16 at 9:53
  • then remove $data from parent::__construct($context, $data); – Marius Jan 16 at 12:06

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.