0

I need to send data from controller to Block, I have used many ways but it does not work. I'm trying this way:

Controller.php

public function execute()
    {

       $order_id = $this->getRequest()->getParam("order_id");
       $order = $this->orderInterface->loadByIncrementId($order_id);
       $pakkeShipment = $order->getPakkeShipment();

        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

        $block = $resultPage->getLayout()
                ->createBlock('Vendor\Pakke\Block\TrackInfo\TrackingInfo')
                ->setTemplate('Vendor_Pakke::track_info.phtml')
                ->setTrackingInfo($pakkeShipment)
                ->toHtml();
        $this->getResponse()->setBody($block);

    }

Block.php

class TrackingInfo extends Template
{
    public $trackingData;

    public function __construct(
        Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function setTrackingInfo($trackingData){
        $this->trackingData = $trackingData;
    }

    public function getTrackingInfo(){
        return $this->trackingData;
    }
}

pakke_index_index.phtml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Vendor\Pakke\Block\TrackInfo\TrackingInfo" name="tracking_info" template="Vendor_Pakke::track_info.phtml" />
    </referenceContainer>
</page>

I get an error in this way, I just want to send $pakkeShipment to setTrackingInfo() so I can use the method getTrackingInfo(); in my phtml.

1 Answer 1

0

you can add your data in below code
One approach is to use the registry so in your controller class you put it in the registry, and then in your block you can retrieve it.

<?php

namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $this->_coreRegistry->register('foo', 'bar');
        return $this->_resultPageFactory->create();
    }
}

Then in your block you can do;

<?php

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Moo extends Template
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    public function getFoo()
    {
        // will return 'bar'
        return $this->_coreRegistry->registry('foo');
    }
}

Obviously you need to get your block on the page in the first place but this should give you a good start.

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.