<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace task\Mycontact\Controller\Index;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
class Post extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
{
 /**
 * @var DataPersistorInterface
 */
 private $dataPersistor;
 /**
 * @var Context
 */
 private $context;
 /**
 * @var LoggerInterface
 */
 private $logger;
 private $mycontactFactory;
 /**
 * @param Context $context
 * @param ConfigInterface $contactsConfig
 * @param MailInterface $mail
 * @param DataPersistorInterface $dataPersistor
 * @param LoggerInterface $logger
 */
 public function __construct(
 Context $context,
 DataPersistorInterface $dataPersistor,
 \Sigma\Mycontact\Model\MyContactFactory $mycontactFactory,
 LoggerInterface $logger = null
 ) {
 parent::__construct($context);
 $this->context = $context;
 $this->dataPersistor = $dataPersistor;
 $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
 $this->mycontactFactory = $mycontactFactory;
 }
 /**
 * Post user question
 *
 * @return Redirect
 */
 public function execute()
 {
// $data = $this->getRequest()->getParams();
// print_r($data);
// exit();
 if (!$this->getRequest()->isPost()) {
 return $this->resultRedirectFactory->create()->setPath('*/*/');
 }
 try {
// $this->validatedParams();
// $this->validatedParams();
// $request = $this->getRequest();
// $data = $request->getParams();
// print_r($data);
// exit();
 $request = $this->getRequest();
 $data = $request->getParams();
 $model =$this->MyContactFactory->create();
 echo "hi";
 $model->setData($data);
 $model->save();
 $this->messageManager->addSuccessMessage(
 __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
 );
 $this->dataPersistor->clear('contact_us');
 } catch (LocalizedException $e) {
 $this->messageManager->addErrorMessage($e->getMessage());
 $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
 } catch (\Exception $e) {
 $this->logger->critical($e);
 $this->messageManager->addErrorMessage(
 __('An error occurred while processing your form. Please try again later.')
 );
 $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
 }
 return $this->resultRedirectFactory->create()->setPath('mycontactform/index');
 }
 /**
 * @return array
 * @throws \Exception
 */
// private function validatedParams()
// {
// $request = $this->getRequest();
// if (trim($request->getParam('name')) === '') {
// throw new LocalizedException(__('Enter the Name and try again.'));
// }
// if (trim($request->getParam('comment')) === '') {
// throw new LocalizedException(__('Enter the comment and try again.'));
// }
// if (false === \strpos($request->getParam('email'), '@')) {
// throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
// }
// if (trim($request->getParam('hideit')) !== '') {
// throw new \Exception();
// }
//
// return $request->getParams();
// }
}
main.CRITICAL: Notice: Undefined property: task\Mycontact\Controller\Index\Post\Interceptor::$MyContactFactory in /var/www/html/magento23/app/code/task/Mycontact/Controller/Index/Post.php on line 84 {"exception":"[object] (Exception(code: 0): Notice: Undefined property:
 Ranjit Shinde
 
 6041 gold badge8 silver badges19 bronze badges
 
 1 Answer 1
You are using the wrong variable in execute function. MyContactFactory start with the small case as like below.
$model = $this->myContactFactory->create();
 answered Dec 17, 2020 at 7:28
 
 
 
 Ranjit Shinde 
 
 6041 gold badge8 silver badges19 bronze badges
 
 - 
 after that again I got this exceptionSampath– Sampath2020年12月17日 07:31:23 +00:00Commented Dec 17, 2020 at 7:31
- 
 [2020年12月17日 07:29:16] main.CRITICAL: Class task\Mycontact\Model\MyContact does not exist {"exception":"[object] (ReflectionException(code: -1): Class task\\Mycontact\\Model\\MyContact does not exist at /var/www/html/magento23/vendor/magento/framework/Code/Reader/ClassReader.php:19)"} []Sampath– Sampath2020年12月17日 07:31:31 +00:00Commented Dec 17, 2020 at 7:31
- 
 @Magento2 either task\Mycontact\Model\MyContact class does not exit or there is some error in this class. Ans also ideally class name should start with upprcase, it should be Task not task.Ranjit Shinde– Ranjit Shinde2020年12月17日 07:45:15 +00:00Commented Dec 17, 2020 at 7:45
- 
 <?php namespace Task\Mycontact\Model; class Mycontact extends \Magento\Framework\Model\AbstractModel { protected function _construct() { $this->_init('Task\Mycontact\Model\ResourceModel\Mycontact'); } // public function getAvailableStatuses() // { // // // $availableOptions = ['1' => 'Enable', // '0' => 'Disable']; // // return $availableOptions; // } }Sampath– Sampath2020年12月17日 08:32:47 +00:00Commented Dec 17, 2020 at 8:32
- 
 that is my Mycontact classSampath– Sampath2020年12月17日 08:33:33 +00:00Commented Dec 17, 2020 at 8:33
default