4

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!

  • try to remove folder var/generator from your root folder – Tuyen Nguyen Dec 10 '18 at 3:39
  • have you ran php bin/magento setup:upgrade? Or delete generated/code/YourModule manually. – Adarsh Khatri Dec 10 '18 at 4:03
6

You have issue at __construct(.

You must pass context object \Magento\Framework\App\Action\Context $context to 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 this changes donot forgot to do Di compile

11

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.

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.