2

I am trying to define 2 models inside controller constructor to use it later inside execute function. This is my code:

namespace Company\Modul\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;

class Save extends \Magento\Backend\App\Action
{

protected $dataProcessor;
protected $appEmulation;
protected $filterProvider;


public function __construct(
    \Magento\Store\Model\App\Emulation $appEmulation, 
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,      
    Action\Context $context, 
    PostDataProcessor $dataProcessor)
{
    $this->dataProcessor = $dataProcessor; 
    $this->filterProvider = $filterProvider; 
    $this->appEmulation = $appEmulation;     
    parent::__construct($context);        
}

public function execute()
{
    $data = $this->getRequest()->getPostValue();
    $this->appEmulation->startEnvironmentEmulation(1);
    //some more code...
    $this->_redirect('*/*/');
}
}

I am getting this error:

Recoverable Error: Argument 2 passed to Company\Module\Controller\Adminhtml\Index\Save::__construct() must be an instance of Magento\Cms\Model\Template\FilterProvider, instance of Magento\Backend\App\Action\Context given, called in C:\wamp\www\magento2b\var\generation\Company\Module\Controller\Adminhtml\Index\Save\Interceptor.php on line 14 and defined in C:\wamp\www\magento2b\app\code\Company\Module\Controller\Adminhtml\Index\Save.php on line 16

What am I doing wrong, what would be a proper way to access filterProvider?

1 Answer 1

8

Replace __construct method following code:


public function __construct(
    Action\Context $context,
    \Magento\Store\Model\App\Emulation $appEmulation,
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    PostDataProcessor $dataProcessor
) {
    $this->dataProcessor = $dataProcessor;
    $this->filterProvider = $filterProvider;
    $this->appEmulation = $appEmulation;
    parent::__construct($context);
}

Note:

For Magento 2.0.x and 2.1.x

You need to remove var/generation/* folder content

For Magento 2.2.x ( generation folder is moved out from var folder and rename as generated in Magento 2.2.x )

You need to remove generated/* folder content except .htaccess file.

2
  • Thank you, I didn't know that context must be passed as first argument, I thought the order isn't important. Now I know tnx to you.
    – JohnyFree
    Commented Jul 4, 2016 at 19:10
  • 2
    @JohnyFree the order is always based on the parameter of the original constructor, extra parameters go in the end Commented Jul 4, 2016 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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