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\InterceptorException #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!
Best Answer
You have an issue at
__construct(
.You must pass the context object
\Magento\Framework\App\Action\Context $context
to the parent classMagento\Framework\App\Action\Action
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