0

I got this controller:

<?php    
class MyClass extends \Magento\Sales\Controller\Adminhtml\Order
{
    public function execute()
    {     
        echo "TEST";
    }
}
?>    

I want it to call a template. How can I do that?

2
  • 1
    create handler on layout file and call phtml. Jul 3, 2018 at 12:01
  • From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.
    – Ben Crook
    Jul 3, 2018 at 12:07

2 Answers 2

7

check the below code

 /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * @param Context     $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory

    ) {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);

    }

    public function execute()
    {

        $resultPage = $this->_resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

        $block = $resultPage->getLayout()
                ->createBlock('Companyname\Module\Block\blockname')
                ->setTemplate('Companyname_Module::test.phtml')
                ->toHtml();
        $this->getResponse()->setBody($block);
    }
}
0

In controller:

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use OX\OfferNotifier\Helper\Data;

class Index extends Action
{

    protected $_resultPageFactory;
    protected $_resultJsonFactory;
    protected $_helperData;

    public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)
    {
        parent::__construct($context);
        $this->_resultPageFactory = $resultPageFactory;
        $this->_resultJsonFactory = $resultJsonFactory;
        $this->_helperData = $data;
    }

    public function execute()
    {
        if ($this->_helperData->isOfferNotifierEnabled()) {
            $result = $this->_resultJsonFactory->create();
            $resultPage = $this->_resultPageFactory->create();
            $productId = $this->getRequest()->getParam("productId");
            $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
            $url = $this->getRequest()->getParam("url") ?? 0;
            //Get the product id for ProductView Page 
            if ($offerInProductView) {
                $productInfo = $resultPage->getLayout()
                        ->createBlock('OOO\YYY\Block\Product\View')
                        ->setData('product_id', $productId)
                        ->setData('offer_in_product_view', $offerInProductView)
                        ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
                        ->toHtml();
            }


            $result->setData(['content' => $productInfo]);
            return $result;
        }
    }

}

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.