1

I call controller function from phtml via ajax, it calls controller function Now i am trying to call block function in controller it is not working

Controller code

 public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->_registry = $registry;
        $this->_resultLayoutFactory = $resultLayoutFactory;
    }

    public function execute()
    {
        if($this->getRequest()->isAjax()){
            $start = $_POST['start'];
            $end = $_POST['end'];

            $enddate = date('Y-m-d 23:55:55', strtotime($end));
            $startdate = date('Y-m-d 00:00:00', strtotime($start));
            $resultLayout = $this->_resultLayoutFactory->create();
            $response = $resultLayout->getLayout()->getBlock('My\Module\Block\dashboard')->getprocesseddata();   
            return $response;         
        }

}

In block

 public function __construct(
        Template\Context $context,
        CardsManageFactory $cardsManageFactory,
        Data $helperData,
        array $data = [],        
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,

        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Backend\Model\Session $session,
        \Magento\Framework\Registry $registry
    )
    {

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

        parent::__construct($context, $data);

        $this->_cardsManageFactory = $cardsManageFactory;
        $this->_helperData         = $helperData;
        $this->scopeConfig = $scopeConfig; // get api_key and secret_key
        $this->session = $session;
        $this->_registry = $registry;
    }

 public function getprocesseddata()
    {
      return 1;
}

while call controller execute from ajax . call is working. but from controller i cant call block

0

Try below code in controller.

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\View\LayoutInterface $layout
) {
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
    $this->_registry = $registry;
    $this->layout = $layout;
}

public function execute()
{
    if($this->getRequest()->isAjax()){
        $start = $_POST['start'];
        $end = $_POST['end'];

        $enddate = date('Y-m-d 23:55:55', strtotime($end));
        $startdate = date('Y-m-d 00:00:00', strtotime($start));
        $response = $this->layout->getBlock('My\Module\Block\dashboard')->getprocesseddata();   
        return $response;         
    }

}

I hope it'll help :)

  • Still i got the error @RamkishanSuthar – Sammu Sundar Dec 17 '18 at 14:07
  • I got error the server responded with a status of 500 (Internal Server Error). – Sammu Sundar Dec 17 '18 at 14:09
  • One more thing controller is the admin controller – Sammu Sundar Dec 17 '18 at 14:12
  • In inspector preview didnt show anything – Sammu Sundar Dec 17 '18 at 14:15
  • looks like you are on production mode. check logs – Ramkishan Suthar Dec 17 '18 at 14:31

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.