2

A page is submitted with the Voucher Number to check if it is valid, and if it is, then it the user moves forward in the process. How can I pass this value to the phtml to use again? Preferably I would like to pass it back as part of the model.

   public function execute()
    {
        // Get submitted voucher number
        $voucherNumber = $_POST['voucher-number'];

        // Validate the voucher number
        if($this->_objectManager->create('Hidden\Hidden\Helper\VoucherHelper')->validate($voucherNumber)){
            // Valid
            // ******** NEED TO RETURN $voucherNumber here
            return $this->resultPageFactory->create();
        }
        else {
            // Invalid
            return $this->_redirect('hidden/voucher');
        }
    }

I'm aware of this answer and this answer but both are related to Magento 1 and I want to make sure that they are still correct now. Further more these are related to blocks and I would think (although wrongly possibly) I can pass the model in from the controller action?

1 Answer 1

1

--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');

4
  • Thanks although is it possible to pass a whole model to a page?
    – tim.baker
    Commented Aug 15, 2016 at 15:07
  • Yes, we should use \Magento\Framework\Registry to pass whole model. Commented Aug 15, 2016 at 15:17
  • Amy chance you could give an example in your answer - not sure how to approach this?
    – tim.baker
    Commented Aug 15, 2016 at 15:27
  • Yes, I updated my answer. Commented Aug 15, 2016 at 16:01

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.