Magento 2.2 – Helper Class Not Found in Custom Module

extensionshelpermagento2magento2.2module

I'm creating a payment module and I'm having a curious problem. I started by creating the module inside the app/code/Vendorname/Payment folder for convenience. It is not yet complete, but the system.xml settings options were working correctly and all configurations can be changes in admin.

One of the system.xml fields has a custom source model, where I used the module's helper and everything was ok.

So for now I created a repository in github, and put the module so that I could use the composer (and remove it from the app/code folder). I did this by thinking of leaving the module in a way that can be replicated in other stores, so with better distribution way.

However after installing with composer (and after did all upgrade and compile di things on shell), when accessing the configuration section in the admin panel I always get the error below.

Fatal error: Uncaught Error: Class 'Vendorname\Payment\Helper\Data' not found in /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:111 
Stack trace: 
#0 /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Vendorname\\Payment\\...', Array) 
#1 /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/Factory/Compiled.php(150): Magento\Framework\ObjectManager\Factory\Compiled->create('Vendorname\\Payment\\...') 
#2 /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/Factory/Compiled.php(79): Magento\Framework\ObjectManager\Factory\Compiled->get('Vendorname\\Payment\\...') 
#3 /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Compiled->create('Vendorname\\Payment\\...') 
#4 /Users/username/sites/vendornamem2/vendor/magento/module-config/Model/Config/SourceFactory.php(37): Magento\Fr in /Users/username/sites/vendornamem2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111

Apparently the helper is not being found in my custom field class. They follow the source model of the field in question (Mode).

<?php
namespace Vendorname\Payment\Model\Source;

class Mode implements \Magento\Framework\Option\ArrayInterface
{
    /**
    * @var \Vendorname\Payment\Helper\Data
    */
    protected $_paymentHelper;

    /**
    * Mode constructor.
    *
    * @param \Vendorname\Payment\Helper\Data $paymentHelper
    */
    public function __construct(
        \Vendorname\Payment\Helper\Data $paymentHelper
    ) {
        $this->_paymentHelper = $paymentHelper;
    }

    public function toOptionArray()
    {
        /** @var \Vendorname\Payment\Helper\Data $VendornameHelper */
        $VendornameHelper = $this->_paymentHelper;
        return [
            ['value' => $VendornameHelper::ONPAGE_MODE, 'label' => 'On Page'],
            ['value' => $VendornameHelper::IFRAME_MODE, 'label' => 'Iframe'],
            ['value' => $VendornameHelper::REDIRECT_MODE, 'label' => 'Redirect']
        ];
    }
}

The error came after simply by migrating the code from the app/code folder to the composer. Not even one character was modified in the process. Is there any helper mapping that needs to be done?

Best Answer

I got it.

I forgot map folder Vendorname\Payment on composer.json (psr-4 node). I added the following code and it works.

"autoload": {
  "files": [
    "registration.php"
  ],
  "psr-4": {
    "Vendorname\\Payment\\": ""
  }
}
Related Topic