If your controller extend Magento\Framework\App\Action\Action
you should follow rules of creation of the controller. So you should implement dependency injection of the parent class constructor (Magento\Framework\App\Action\Action
):
/**
* @param Context $context
*/
public function __construct(Context $context)
{
parent::__construct($context);
$this->_objectManager = $context->getObjectManager();
$this->_eventManager = $context->getEventManager();
$this->_url = $context->getUrl();
$this->_actionFlag = $context->getActionFlag();
$this->_redirect = $context->getRedirect();
$this->_view = $context->getView();
$this->messageManager = $context->getMessageManager();
}
Your construct method should look like this after a modifications:
/**
* @param Context $context
* @param UrlInterface $urlBuilder
*/
public function __construct(
Context $context,
UrlInterface $urlBuilder
) {
parent::__construct($context);
$this->urlBuilder = $urlBuilder;
}
NOTE: do not forget to remove old var/generation
directory after implementing of modifications, because there are stored the generated files of controllers.
It's caused by the fact that your model constructor does not match your original model constructor.
So this is the constructor from Magento\Newsletter\Model\Queue
:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Newsletter\Model\Template\Filter $templateFilter,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
\Magento\Newsletter\Model\TemplateFactory $templateFactory,
\Magento\Newsletter\Model\ProblemFactory $problemFactory,
\Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
\Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$resource,
$resourceCollection,
$data
);
$this->_templateFilter = $templateFilter;
$this->_date = $date;
$this->_templateFactory = $templateFactory;
$this->_problemFactory = $problemFactory;
$this->_subscribersCollection = $subscriberCollectionFactory->create();
$this->_transportBuilder = $transportBuilder;
}
To fix your constructor it needs to be:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Newsletter\Model\Template\Filter $templateFilter,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
\Magento\Newsletter\Model\TemplateFactory $templateFactory,
\Magento\Newsletter\Model\ProblemFactory $problemFactory,
\Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
\Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
\Vendor\Module\Block\Testing $customergroup,
\Magento\Customer\Model\ResourceModel\Group\Collection $customergroupdata,
array $data = []
)
{
$this->_customergroup = $customergroup;
$this->_customergroupdata = $customergroupdata;
parent::__construct(
$context,
$registry,
$templateFilter,
$date,
$templateFactory,
$problemFactory,
$subscriberCollectionFactory,
$transportBuilder,
$resource,
$resourceCollection,
$data
);
}
Best Answer
I hope you have already solved this question, it would be nice to post the best way you used it.
Currently, I have solved with the creation of Magento factories. Example:
The third party lib depends on an argument:
$apiKek
In your custom module, in the class that will need this instantiated object: