- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020
11:45 PM
05-18-2020
11:45 PM
Send data from controller to phtml
how to send data from controller to phtml and how to access it ?
Labels:
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2020
12:53 PM
05-24-2020
12:53 PM
Re: Send data from controller to phtml
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2020
04:47 AM
05-31-2020
04:47 AM
Re: Send data from controller to phtml
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!
Problem solved? Click Accept as Solution!