Skip to content

Put the controller json return into the phtml

I got a task to create a module and a new controller that takes orderID as a parameter and returns a JSON with information. I have created a controller and can show the JSON return, but how can I render it to the phtml?

app/code/Mymodule/OrderController/Controller/Index/OrderControl.php

<?php

namespace MymoduleOrderControllerControllerIndex;

use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultFactory;
use MagentoSalesModelResourceModelOrderCollectionFactory;
use MagentoFrameworkControllerResultJsonFactory;

class OrderControl extends MagentoFrameworkAppActionAction {

protected $orders;

protected $collectionFactory;

protected $resultFactory;

protected $jsonFactory;

public function __construct(
    Context $context,
    Order $orders,
    CollectionFactory  $collectionFactory,
    ResultFactory $resultFactory,
    JsonFactory $jsonFactory

)
{
    $this->orders=$orders;
    $this->collectionFactory=$collectionFactory;
    $this->resultFactory=$resultFactory;
    $this->jsonFactory=$jsonFactory;
    parent::__construct($context);
}


public function execute()
{

    $collection = $this->collectionFactory->create()->addAttributeToSelect('*')->getData();
    $result = $this->jsonFactory->create();
    $data = $result->setData($collection);

    return $data;
  }
}