11

Sorry I am new to M2, I am having error whenever I load helper class into controller. I searched every online but no luck. Here is the setup and error when I load

Setup: Magento 2.3 Server Centos 7 / PHP7.2 / Local VM

Error Trace

1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Custom\Payment\Controller\Index\Index\Interceptor

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Custom\Payment\Controller\Index\Index\Interceptor #0 /var/www/html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(66): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Custom\Payment\...', Array) #1 /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Custom\Payment\...', Array) #2 /var/www/html/vendor/magento/framework/App/ActionFactory.php(44): Magento\Framework\ObjectManager\ObjectManager->create('Custom\Payment\...') #3 /var/www/html/vendor/magento/framework/App/Router/Base.php(304): Magento\Framework\App\ActionFactory->create('Custom\Payment\...') #4 /var/www/html/vendor/magento/framework/App/Router/Base.php(165): Magento\Framework\App\Router\Base->matchAction(Object(Magento\Framework\App\Request\Http), Array) #5 /var/www/html/vendor/magento/framework/App/FrontController.php(95): Magento\Framework\App\Router\Base->match(Object(Magento\Framework\App\Request\Http)) #6 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http)) #7 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array) #8 /var/www/html/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Request\Http)) #9 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http)) #10 /var/www/html/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Request\Http)) #11 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http)) #12 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Request\Http)) #13 /var/www/html/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, NULL) #14 /var/www/html/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http)) #15 /var/www/html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch() #16 /var/www/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http\Interceptor->launch() #17 /var/www/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor)) #18 {main}

Helper Code

namespace Custom\Payment\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class General extends AbstractHelper
{

    public function RandomFunc()
       {
               echo "This is Helper in Magento 2";
       }

}

Controller Code

namespace Custom\Payment\Controller\Index;

use Custom\Payment\Helper\General as CustomHelper;
use Magento\Framework\App\Action\Action;

class Index extends Action
{
    protected $helper;

    public function __construct(
        CustomHelper $helper
    ) {
        $this->helper = $helper;
    }

    public function execute()
    {
        $this->helper->RandomFunc();
    }
}

Anyone could enlighten me where I did wrong?

Thank you!

2
  • try to remove folder var/generator from your root folder Commented Dec 10, 2018 at 3:39
  • have you ran php bin/magento setup:upgrade? Or delete generated/code/YourModule manually. Commented Dec 10, 2018 at 4:03

5 Answers 5

20

You have an issue at __construct(.

You must pass the context object \Magento\Framework\App\Action\Context $context to the parent class Magento\Framework\App\Action\Action

public function __construct(
\Magento\Framework\App\Action\Context $context,
    CustomHelper $helper
) {
    $this->helper = $helper;
parent::__construct($context);
}

after these changes, when in developer mode, just remove the folders under var/generation/ or generated/ as the documentation suggests

or run setup:di:compile when in production mode


1
  • ./bin/magento setup:di:compile was the thing, thanks
    – Klesun
    Commented Jan 29, 2020 at 20:47
25

Just Remove the Generation folder or in version 2.2 remove the generated folder.

rm -rf generated/

Flush the cache. It should work then. Its because the interceptor file needs to be re-created or refresh.

1

In addition to Amit Bera's answer, make sure that if you're adding any new parameters to the __construct() method that you add them before any parameters with default values (like null) defined.

0

Run the di complie and cache flush commands

php bin/magento s:d:c

Php bin/magento c:c

Php bin/magento c:f
0

The interceptor file needs to be re-create. So kindly run di:compile and clean cache

php bin/magento setup:di:compile
php bin/magento  c:f

//Or 

rm -rf generated/

Its worked for me

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.