Magento2 – Admin Form Controller Error

admin-controlleradminformmagento2

So i have a controller to access form with structure like this:

Controller>Adminhtml>Preferences>NewAction.php

but when i try to acces /preferences/new, it gives me an error like this:

Fatal error: Uncaught TypeError: Argument 1 passed to
Lime\Courier\Controller\Adminhtml\Preferences\NewAction::__construct()
must be an instance of Magento\Backend\App\Action\Context, instance of
Magento\Framework\ObjectManager\ObjectManager given, called in
C:\xampp7\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php
on line 93 and defined in
C:\xampp7\htdocs\magento2\app\code\Lime\Courier\Controller\Adminhtml\Preferences\NewAction.php:16
Stack trace: #0

here's my controller NewAction.php:

namespace Lime\Courier\Controller\Adminhtml\Preferences;

class NewAction extends \Lime\Courier\Controller\Adminhtml\Preferences
{

    protected $resultForwardFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
    ) {
        $this->resultForwardFactory = $resultForwardFactory;
        parent::__construct($context, $coreRegistry);
    }

    /**
     * New action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
        $resultForward = $this->resultForwardFactory->create();
        return $resultForward->forward('edit');
    }
}

Best Answer

As Magento compile the classes and put them in var/generation folder, So it will always be a better practice to delete var/generation and var/cache folder, whenever you created a new class or change any parameters of constructor of any class.

If you don't want to delete whole folder then try to delete files from the same class path from where you are getting error in generation folder, hopefully you will be able to debug the problem.

Another solution is to compile your code by setup:di:compile command but in developing phase it become cumbersome for developer to run this time taking command to just get a simple solution :) :)

Related Topic