--We can use \Magento\Framework\Registry in your Block or Controller.
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
...
$this->_coreRegistry = $registry;
...
$this->_coreRegistry->register('voucherNumber', $voucherNumber);
//We can get
$this->_coreRegistry->registry('voucherNumber');
--After loading layout, get the block and set values in Controller.
/**
* @var \Magento\Framework\App\ViewInterface $_view
*/
/**
* @return void
*/
public function execute()
{
$voucherNumber = 24566;
$this->_view->loadLayout();
$block = $this->_view->getLayout()->getBlock('your_block_name');
if($block) {
$block->setVoucherNumber($voucherNumber);
}
$this->_view->renderLayout();
}
--We can use setData() or getData() in our Block.
//In your Block:
public function getVoucherNumber()
{
if (!$this->hasData('voucherNumber')) {
$this->setData('voucherNumber', $voucherNumber);
}
return $this->getData('voucherNumber');
}
//In your template:
<?php $voucherNumber = $block->getVoucherNumber();?>
Sample: Pass model object via controller.
<?php
namespace Vendor\Module\Controller\Test;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var \Magento\Framework\Controller\ResultFactory
*/
protected $_resultPageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\Registry $coreRegistry
)
{
$this->_coreRegistry = $coreRegistry;
$this->_resultPageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
//Get ID and create model
$entityId = $this->getRequest()->getParam('entity_id');
$model = $this->_objectManager->create('Vendor\Module\Model\VoucherEntity');
if($entityId) {
$model->load($entityId);
if(!$model->getId()) {
$this->messageManager->addError(__('This entity no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->_resultPageFactory->create();
//Register model to use later in blocks
$this->_coreRegistry->register('voucher_entity', $model);
return $resultPage;
}
}
In your Block, you can get:$this->_coreRegistry->registry('voucher_entity')
;
You can create function in block example as below:
public function getEvents(){
//get values of current page
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
//get values of current limit
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
$collection = $this->_eventCollectionFactory->create();
$collection->addFieldToFilter('is_active',1);
$collection->setPageSize($pageSize);
$collection->setCurPage($page);
$collection->getSelect()->order('main_table.start_time asc');
return $collection;
}
Using below code you can call this function in phtml file
$block->getEvents();
EDIT: How to get post data in block. you need to add below code in block
protected $request;
public function __construct(
\Magento\Framework\App\Request\Http $request,
....
) {
$this->request = $request;
}
public function getPostData()
{
// you can use below code in any function
$postData = $this->getRequest()->getParams();
}
Best Answer
There are a couple of ways to do this.
Assign directly to the Block:
or
Then you can access it in the phtml file like this:
Use the Mage registry:
and then use it like: