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?
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?
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);
}
}
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;
}
}
}