I got this error while saving image
Fatal error: Call to a member function create() on null in /opt/lampp/htdocs/magento/2.1/app/code/Contact/Modules/Controller/Contact/Index.php on line 85
here is my controller
<?php
/**
*
* Copyright © 2015 Contactcommerce. All rights reserved.
*/
namespace Contact\Modules\Controller\Contact;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $_cacheTypeList;
/**
* @var \Magento\Framework\App\Cache\StateInterface
*/
protected $_cacheState;
/**
* @var \Magento\Framework\App\Cache\Frontend\Pool
*/
protected $_cacheFrontendPool;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $_objectManager;
protected $adapterFactory;
protected $uploader;
protected $filesystem;
/**
* @param Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
* @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\StateInterface $cacheState,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Image\AdapterFactory $adapterFactory,
\Magento\MediaStorage\Model\File\UploaderFactory $uploader,
\Magento\Framework\Filesystem $filesystem,
array $data = []
) {
parent::__construct($context, $data);
$this->_objectManager = $objectManager;
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheState = $cacheState;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->resultPageFactory = $resultPageFactory;
}
/**
* Flush cache storage
*
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
// $email = $this->getRequest()->getParam('email');
// $subject = $this->getRequest()->getParam('subject');
// $message = $this->getRequest()->getParam('message');
// $photo = $this->getRequest()->getParam('photo');
if(isset($post))
{
$contact = $this->_objectManager->create('Contact\Modules\Model\Contact');
$contact->setData('email', $post['email']);
$contact->setData('subject', $post['subject']);
$contact->setData('message', $post['message']);
if (isset($_FILES['photo']) && isset($_FILES['photo']['name']) && strlen($_FILES['photo']['name']))
{
$base_media_path = 'Contact/Modules/images';
$uploader = $this->uploader->create(
['fileId' => 'photo']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$imageAdapter = $this->adapterFactory->create();
$uploader->addValidateCallback('photo', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory->getAbsolutePath(base_media_path));
}
$post['photo'] = base_media_path.$result['file'];
$contact->setData('photo', $post['photo']);
$contact->save();
}
else
{
$this->resultPage = $this->resultPageFactory->create();
return $this->resultPage;
}
}
}
Please suggest me solution of this error..
2 Answers 2
You forgot to declare some protected variables, you need to add the following
protected $uploader;
protected $adapterFactory;
protected $filesystem;
And also add the following to your __construct() :
$this->uploader = $uploader;
$this->adapterFactory = $adapterFactory;
$this->filesystem = $filesystem;
-
I have already declared. By mistake it is not showing on question.Ramkishan Suthar– Ramkishan Suthar2016年10月04日 07:14:24 +00:00Commented Oct 4, 2016 at 7:14
-
sorry sorry i actually forgot ;)Ramkishan Suthar– Ramkishan Suthar2016年10月04日 07:15:08 +00:00Commented Oct 4, 2016 at 7:15
-
@Ramkishan not a problem ;)Raphael at Digital Pianism– Raphael at Digital Pianism2016年10月04日 07:17:02 +00:00Commented Oct 4, 2016 at 7:17
Use below code:
<?php
/**
*
* Copyright © 2015 Contactcommerce. All rights reserved.
*/
namespace Contact\Modules\Controller\Contact;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $_cacheTypeList;
/**
* @var \Magento\Framework\App\Cache\StateInterface
*/
protected $_cacheState;
/**
* @var \Magento\Framework\App\Cache\Frontend\Pool
*/
protected $_cacheFrontendPool;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $_objectManager;
protected $adapterFactory;
protected $uploader;
protected $filesystem;
/**
* @param Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
* @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\StateInterface $cacheState,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Image\AdapterFactory $adapterFactory,
\Magento\MediaStorage\Model\File\UploaderFactory $uploader,
\Magento\Framework\Filesystem $filesystem,
array $data = []
) {
parent::__construct($context, $data);
$this->_objectManager = $objectManager;
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheState = $cacheState;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->uploader = $uploader;
$this->resultPageFactory = $resultPageFactory;
}
/**
* Flush cache storage
*
*/
public function execute()
{
$post = $this->getRequest()->getPostValue();
// $email = $this->getRequest()->getParam('email');
// $subject = $this->getRequest()->getParam('subject');
// $message = $this->getRequest()->getParam('message');
// $photo = $this->getRequest()->getParam('photo');
if(isset($post))
{
$contact = $this->_objectManager->create('Contact\Modules\Model\Contact');
$contact->setData('email', $post['email']);
$contact->setData('subject', $post['subject']);
$contact->setData('message', $post['message']);
if (isset($_FILES['photo']) && isset($_FILES['photo']['name']) && strlen($_FILES['photo']['name']))
{
$base_media_path = 'Contact/Modules/images';
$uploader = $this->uploader->create(
['fileId' => 'photo']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$imageAdapter = $this->adapterFactory->create();
$uploader->addValidateCallback('photo', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory->getAbsolutePath(base_media_path));
}
$post['photo'] = base_media_path.$result['file'];
$contact->setData('photo', $post['photo']);
$contact->save();
}
else
{
$this->resultPage = $this->resultPageFactory->create();
return $this->resultPage;
}
}
}
You miss the define variable in construct so you got that error.
-
This answer is identical to other answer. kindly don't post the identical answer.Qaisar Satti– Qaisar Satti2016年10月04日 07:55:37 +00:00Commented Oct 4, 2016 at 7:55
-
@QaisarSatti I have not seen answer, while I am writing answer at that time other answer was posted.In both answer have slight time differencePrashant Valanda– Prashant Valanda2016年10月04日 08:58:43 +00:00Commented Oct 4, 2016 at 8:58
Explore related questions
See similar questions with these tags.