skip to Main Content

I try to call a function from a module A to a module B
here is the module A code

namespace AEpaymentModel;
  class Etransactions
    {
      public function customPayment{
        return "test";
      }

and module b code

  namespace BPaymentControllerIndex;

class Payment extends MagentoFrameworkAppActionAction
{
    protected $_pageFactory;
    protected $_transaction;

    public function __construct(
        MagentoFrameworkAppActionContext $context,
        MagentoFrameworkViewResultPageFactory $pageFactory,
        ETransactionsEpaymentModelEtransactions $transaction
    )
    {
        $this->_pageFactory = $pageFactory;
        $this->_transaction = $transaction;
        parent::__construct($context);
    }

    public function execute()
    {
        echo "Hello World".PHP_EOL;
        $foo="a";
        echo $foo;
        echo $this->_transaction->customPayment();
        //echo $this->customPayment();
        echo $foo;

        exit;
    }
}

this code return the “hello world”, the first $foo, not the second and doesn’t display any error

can someone explain me where is my error ?

EDIT: i didn’t change anything but it works fine now.
thanks for the answers anyway

2

Answers


  1. The object you want create the path your are injecting is incorrect.

     public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoFrameworkViewResultPageFactory $pageFactory,
            AEpaymentModelEtransactions $transaction // changes are here
        )
        {
            $this->_pageFactory = $pageFactory;
            $this->_transaction = $transaction;
            parent::__construct($context);
        }
    

    Kindly use exception handling.

    try{
    $this->_transaction->customPayment();
    }catch(Exception $e){
    //log your exception here.
    }
    
    Login or Signup to reply.
  2. In Magento, Helper classes are available to use anywhere (Block, Controller, Model, Observer, View). So you should create a method in helper class and call it anywhere by the following way.

    Declar the helper class and method: ModuleAEpaymentHelperData.

    <?php
    namespace ModuleAEpaymentHelper;
    
    class Data extends MagentoFrameworkAppHelperAbstractHelper
    {
        public function yourHelperMethod()
        {
            # code...
        }
    }
    

    Call the method:

    $helper = $this->_objectManager->create(ModuleAEpaymentHelperData::class);
    $helper->yourHelperMethod();
    

    Note: If the object manager is not injected in your class. Please follow the steps below:

    1) declare private property:

    private $_objectManager;
    

    2) inject in the constructor to initialize:

    public function __construct(
        MagentoFrameworkObjectManagerInterface $objectmanager
    ) {
        $this->_objectManager = $objectmanager;
    }
    

    3) use in some method:

    public function someMethod() {
        $helper = $this->_objectManager->create(ModuleAEpaymentHelperData::class);
        $helper->yourHelperMethod();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search