0

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 Mymodule\OrderController\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Controller\Result\JsonFactory;

class OrderControl extends \Magento\Framework\App\Action\Action {

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;
  }
}
1
  • If you want to use it in phtml file then why not pass an Array or Collection object ? Nov 20, 2022 at 11:21

1 Answer 1

0

Update your controller with below code:

<?php

namespace Mymodule\OrderController\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;

class OrderControl extends \Magento\Framework\App\Action\Action {

    protected $orders;

    protected $collectionFactory;

    protected $resultFactory;

    protected $jsonFactory;

    public function __construct(
        Context $context,
        Order $orders,
        CollectionFactory $collectionFactory,
        ResultFactory $resultFactory,
        JsonFactory $jsonFactory,
        PageFactory $resultPageFactory
    )
    {
        $this->orders=$orders;
        $this->collectionFactory=$collectionFactory;
        $this->resultFactory=$resultFactory;
        $this->jsonFactory=$jsonFactory;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }


    public function execute()
    {
        $collection = $this->collectionFactory->create()->addAttributeToSelect('*')->getData();
        $result = $this->jsonFactory->create();

        $resultPage = $this->resultPageFactory->create();
        $block = $resultPage->getLayout()
                ->createBlock('Mymodule\OrderController\Block\Index\View')
                ->setTemplate('Mymodule_OrderController::view.phtml')
                ->setData($collection)
                ->toHtml();

        $result->setData(['output' => $block]);

        return $result;
    }
}

Create a block at app/code/Mymodule/OrderController/Block/Index/View.php

<?php
namespace Mymodule\OrderController\Block\Index;

use Magento\Framework\View\Element\Template;
 
class View extends Template
{
    public function __construct(Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }
    
    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

Now create a phtml file at app/code/Mymodule/OrderController/view/frontend/templates/view.phtml

<?php

$data = $block->getData();

var_dump($data); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.