Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top