0

I am trying to call function from other controller . I am using version 2.2.6 . While call it is working properly without arugument. If i send argument it provide error should be compatible with

First controller

<?php
namespace Namespace\Module\Controller\Index;


use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\ModuleFactory;
use Magento\Sales\Api\OrderRepositoryInterface;

use Magento\Framework\Setup\SchemaSetupInterface;


class Fetchorder extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_orderCollectionFactory;
    protected $_categoryFactory;
    protected $_order;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Namespace\Module\Controller\Index\Order $order
    )
{

    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_categoryFactory = $categoryFactory;
    $this->_order = $order;

    return parent::__construct($context);
}
public function execute($) 
{   

    $orderCollection = $this->_orderCollectionFactory->create()->addFieldToSelect(array('*'));
    $orderCollection->setOrder('entity_id', 'DESC');
    $orderCollection->setPageSize(10);


    foreach($orderCollection as $order)
    {

    $this->_order->getrecord($order->getId());  // without passing arugument it works.
     }
}

}

Second controller

<?php


namespace Namespace\Module\Controller\Index;


use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\ModuleFactory;
use Magento\Sales\Api\OrderRepositoryInterface;

use Magento\Framework\Setup\SchemaSetupInterface;


class Order extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_orderCollectionFactory;
    protected $_categoryFactory;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
    )
{

    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_categoryFactory = $categoryFactory;

    return parent::__construct($context);
}

public function getrecord($order_id = "") // without order id it call properly
{       
    echo "I am in <br/>";
}

}

How do i pass the arugument .

  • Can you please upload both file full code? – Rohan Hapani Nov 27 '18 at 12:51
  • @RohanHapani , i have updated the code – Sammu Sundar Nov 27 '18 at 12:59
  • Where did you add the dependency for $this->_ordercron in first controller and how are you getting the value of order id ($order->getId()), I can't see the $order object called or order loaded in your first controller file. Provide more details to help you in better and right way.. – Himmat Paliwal Nov 27 '18 at 13:26
  • @HimmatPaliwal , i have updated my code – Sammu Sundar Nov 27 '18 at 13:30
1

You need to use atleast execute() function in controller file. Without execute() function it will not working :

Use code like below way :

=> Controller 1 :

<?php
namespace RH\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;
    /**      * @param \Magento\Framework\App\Action\Context $context      */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {  
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Controller 1'));
        return $resultPage;
    }
    public function getCallFunction($param = "")
    {
        echo "Controller 1 : ".$param;
    }
}

=> Controller 2 :

<?php
namespace RH\HelloWorld\Controller\Index;

class Post extends \Magento\Framework\App\Action\Action
{
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;
    protected $_index;
    /**      * @param \Magento\Framework\App\Action\Context $context      */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \RH\HelloWorld\Controller\Index\Index $index
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_index = $index;
        parent::__construct($context);
    }

    /**
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
        $param = "Controller 2";
        $this->_index->getCallFunction($param);
    }
}

Now, when you run post controller then, output be like :

Controller 1 : Controller 2

Hope, It will helpful for you.

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.