how to send data from controller to phtml and how to access it ?
Hello @shailesh_jadav
Please follow this blog. You will find the solution to your problem.
If it helps then Click on Kudos and Accept as Solution.
Thanks,
Jignesh Chauhan
Please follow below steps to pass data from controller to phtml file:
1. create controller layout "frontname_controller_page_view.xml" file:
<block class="Magento\Framework\View\Element\Template" name="testing.first.layout.example" template="Frontname_Controller::example.phtml" />
2. The $blockvariable is now refer to a Templateclass instance. Use under controller function:
public function execute() { /** @var Page $page */ $page = $this->resultFactory->create(ResultFactory::TYPE_PAGE); /** @var Template $block */ $block = $page->getLayout()->getBlock('testing.first.layout.example'); $block->setData('custom_parameter', 'Data from the Controller'); return $page; }
3. phtml Template Logic example.phtml file:
<?php use Magento\Framework\View\Element\Template; /** @var Template $block */ ?> <h1>My First Template</h1> <p>With Layout support</p> <p><?= $block->getData('custom_parameter'); ?></p>
Image with result:
Issue resolved ?
Please click on Kudos & Accept as Solution!