The object manager is not the correct way to do this. Use dependency injection via the constructor whenever possible.
You are getting that error because your controller extends another class, but you are not following the parent class's constructor, or you did not clear the cache.
You have not provided your code, but I assume your controller extends \Magento\Framework\App\Action\Action
. If you open that, you'll see the constructor signature for that class is:
/**
* @param \Magento\Framework\App\Action\Context $context
*/
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
// ...
}
You're getting the error because Magento is trying to inject a Context object, and you're asking for a Customer. To inject your model, your class constructor needs to look like this:
protected $customer;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Demo\HelloWorld\Model\Customer $customer
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Demo\HelloWorld\Model\Customer $customer
) {
$this->customer = $customer;
parent::__construct($context);
}
Make that change, then flush the Magento cache (or folder var/cache), and then it should work.
Note: This is assuming your Customer class is injectable. If it's loaded from the database, you need to inject CustomerFactory instead, and then call $customer = $this->customerFactory->create()
.
Finally i have resolve my issue .
Only Need to override Subscriber.
I am posting my entire code. may be it will helpful to someone.
di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Newsletter\Model\Subscriber" type="Navin\Subscribe\Model\Subscriber" />
</config>
Subscriber.php Model Files
namespace Navin\Subscribe\Model;
class Subscriber extends \Magento\Newsletter\Model\Subscriber {
/**
* Initialize resource model
*
* @return void
*/
public function subscribe($email, $subscriber_name = '', $subscriber_date_of_birth = '', $subscriber_country_code = '') {
$this->loadByEmail($email);
if (!$this->getId()) {
$this->setSubscriberConfirmCode($this->randomSequence());
}
$isConfirmNeed = $this->_scopeConfig->getValue(
self::XML_PATH_CONFIRMATION_FLAG, \Magento\Store\Model\ScopeInterface::SCOPE_STORE
) == 1 ? true : false;
$isOwnSubscribes = false;
$isSubscribeOwnEmail = $this->_customerSession->isLoggedIn() && $this->_customerSession->getCustomerDataObject()->getEmail() == $email;
if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED || $this->getStatus() == self::STATUS_NOT_ACTIVE
) {
if ($isConfirmNeed === true) {
// if user subscribes own login email - confirmation is not needed
$isOwnSubscribes = $isSubscribeOwnEmail;
if ($isOwnSubscribes == true) {
$this->setStatus(self::STATUS_SUBSCRIBED);
} else {
$this->setStatus(self::STATUS_NOT_ACTIVE);
}
} else {
$this->setStatus(self::STATUS_SUBSCRIBED);
}
$this->setSubscriberEmail($_POST['email']);
}
if(!empty($_POST['subscriber_name']) ){
$this->setSubscriberName($_POST['subscriber_name']); //subscriber_name
$this->setSubscriberDateofbirth($_POST['subscriber_dateofbirth']); //date of birth
$this->setSubscriberCountrycode($_POST['subscriber_countrycode']); //country code
}
if ($isSubscribeOwnEmail) {
try {
$customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
$this->setStoreId($customer->getStoreId());
$this->setCustomerId($customer->getId());
} catch (NoSuchEntityException $e) {
$this->setStoreId($this->_storeManager->getStore()->getId());
$this->setCustomerId(0);
}
} else {
$this->setStoreId($this->_storeManager->getStore()->getId());
$this->setCustomerId(0);
}
$this->setStatusChanged(true);
try {
$this->save();
if ($isConfirmNeed === true && $isOwnSubscribes === false
) {
$this->sendConfirmationRequestEmail();
} else {
$this->sendConfirmationSuccessEmail();
}
return $this->getStatus();
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
}
Best Answer
You need to specify the 3 required Params via Factory Creation Logic of Magento.
Something as follows Inject
\Magento\Sales\Model\ResourceModel\GridFactory $gridInterface
in the constructor and when you use itThis way you can give values to required params of constructor. The first param of Context Object is handled by Magento so that need not be given & in the same way other optional params can also be given a value.