0

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 ?

  • Did you try to flush var/generation folder ? – Raphael at Digital Pianism Feb 15 '17 at 12:23
  • Yes. I have tried that but same error. – Prince Feb 15 '17 at 12:48
  • @Prince have you try below code. – Dhiren Vasoya Feb 15 '17 at 13:13
0

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;
    }
}
  • Thanks for reply. I have updated this code but it gives error in other file. I am trying to solve that. I will come back with solution or new error. – Prince Feb 15 '17 at 12:48
  • If above answer help to solve that error, you can accept so it will help other in community. And for new error you can ask seprate question. – Dhiren Vasoya Feb 15 '17 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.