Question

I have run command bin/magento setup:di:compile in my Magento 2.1.x.

I got below error.

Solwin\ProductAttachment\Controller\Adminhtml\Attachment

Incorrect dependency in class Solwin\ProductAttachment\Controller\Adminhtml\Attachment in /mnt/5BD419690F48D691/projects/work/214magento/app/code/Solwin/ProductAttachment/Controller/Adminhtml/Attachment.php

\Magento\Backend\Model\View\Result\RedirectFactory already exists in context object

File is located at Vendor/Module/Controller/Adminhtml/Attachment.php

<?php

    namespace Vendor\Module\Controller\Adminhtml;

    use Magento\Backend\App\Action;
    use Magento\Backend\App\Action\Context;
    use Magento\Backend\Model\View\Result\RedirectFactory;
    use Magento\Framework\Registry;
    use Vendor\Module\Model\AttachmentFactory;

    abstract class Attachment extends Action
    {

        protected $_attachmentFactory;           
        protected $_coreRegistry;
        protected $_resultRedirectFactory;


        public function __construct(
            AttachmentFactory $attachmentFactory,
            Registry $coreRegistry,
            RedirectFactory $resultRedirectFactory,
            Context $context
        )
        {
            $this->_attachmentFactory     = $attachmentFactory;
            $this->_coreRegistry          = $coreRegistry;
            $this->_resultRedirectFactory = $resultRedirectFactory;
            parent::__construct($context);
        }


        protected function _initAttachment()
        {
            $attachmentId  = (int)$this->getRequest()->getParam('attachment_id');

            $attachment    = $this->_attachmentFactory->create();
            if ($attachmentId) {
                $attachment->load($attachmentId);
            }
            $this->_coreRegistry->register('solwin_productattachment_attachment', $attachment);
            return $attachment;
        }
    }

Could anyone help me to find issue ?

Was it helpful?

Solution

You need to do code like this,

namespace Vendor\Module\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Vendor\Module\Model\AttachmentFactory;

abstract class Attachment extends Action
{

    protected $_attachmentFactory;           
    protected $_coreRegistry;

    public function __construct(
        AttachmentFactory $attachmentFactory,
        Registry $coreRegistry,
        Context $context
    )
    {
        $this->_attachmentFactory     = $attachmentFactory;
        $this->_coreRegistry          = $coreRegistry;
        parent::__construct($context);
    }


    protected function _initAttachment()
    {
        $attachmentId  = (int)$this->getRequest()->getParam('attachment_id');

        $attachment    = $this->_attachmentFactory->create();
        if ($attachmentId) {
            $attachment->load($attachmentId);
        }
        $this->_coreRegistry->register('solwin_productattachment_attachment', $attachment);
        return $attachment;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top