0

I have used the following code

<?php

namespace Inchoo\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Inchoo\Helloworld\Model\PostFactory;

class View extends Action
{


    protected $_modelPostFactory;


    public function __construct(
        Context $context,
        PostFactory $modelPostFactory
    ) {
        parent::__construct($context);
        $this->_modelPostFactory = $modelPostFactory;
    }

    public function execute()
    {

        $postModel = $this->_modelPostFactory->create();

        // Load the item with ID is 1
        $item = $postModel->load(1);
      //  var_dump($item->getData());

        // Get news collection
        $postCollection = $postModel->getCollection();
        // Load all data of collection
         var_dump($postCollection->getData());

    }
}

I have collected the database table field values using the above code. Now i need to send the to block. And need to display in front end

please help me

1
  • It is not proper way to send data controller to block and display in front-end. To fulfill your requirement @Sneha Panchal gives you enough Idea how you can achieve it.
    – Keyur Shah
    Nov 15 '16 at 10:38
1

For sending data from controller to block, you can use registry

Controller

protected $_coreRegistry = null;

public function __construct(
      ....

        \Magento\Framework\Registry $_coreRegistry
    ) {
       .....
        $this->_coreRegistry = $_coreRegistry;
        ....
    }
   public function execute()
    {

        $postModel = $this->_modelPostFactory->create();

        // Load the item with ID is 1
        $item = $postModel->load(1);
      //  var_dump($item->getData());

        // Get news collection
        $postCollection = $postModel->getCollection();
        // Load all data of collection
         var_dump($postCollection->getData());
       $this->_coreRegistry->register('data_test', $postCollection);

    }

Block

 protected $_coreRegistry = null;


public function __construct(
      ....

        \Magento\Framework\Registry $_coreRegistry
    ) {
       .....
        $this->_coreRegistry = $_coreRegistry;
        ....
    }
   public function test()
    {
        $postCollection = $this->_coreRegistry->registry('data_test');
    }
1
  • Usage of Registry is disadvised after Magento 2.3.
    – Max S
    Sep 3 at 10:41
0

Your layout file name must be :

{module name}_{controller name}_{action name}.xml

Then in your layout file please use below code :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
     <body>     
        <referenceContainer name="content">         
            <block class="{company name}\{Module name}\Block\Index" name="block_name">
            </block>
        </referenceContainer>      
    </body>
</page>

In your controller action file use below code :

<?php
namespace {company name}\{module name}\Controller;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $_cacheTypeList;


    protected $_cacheState;

    protected $_cacheFrontendPool;


    protected $resultPageFactory;


    public function __construct(
       \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\StateInterface $cacheState,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
    }


    public function execute()
    {
        $this->resultPage = $this->resultPageFactory->create();  
        return $this->resultPage;

    }
}

Now please create relate block file and create any function like:

    public function getNewsCollection() {
    $postModel = $this->_modelPostFactory->create();

        // Load the item with ID is 1
        $item = $postModel->load(1);
      //  var_dump($item->getData());

        // Get news collection
        $postCollection = $postModel->getCollection();
return $postCollection;
            }

Now in your template file call above function as :

print_r($this->getNewsCollection()->getData());

So in your case you need to load your all collection in block file function and then you can get that collection function in template file same as i explained above.

3
  • Thansks for posting. If i hit the template file it shows only blank page for me. Nov 15 '16 at 13:48
  • i cant access $this->_modelPostFactory->create(); in block file Nov 15 '16 at 15:03
  • first of all check your template file is called or not by echo anything
    – user46386
    Nov 16 '16 at 5:09
0

In controller, define code like below. And mention block name you declared in {route id}_{controller name}_{action name}.xml

$resultPage = $this->resultPageFactory->create();
$resultPage->getLayout()
           ->getBlock('[block_name]')
           ->setDataTest($dataCollection);

Now in template file

<?php
print_r($block->getDataTest()->getData());
?>
1
  • Seems not to work in Magento 2.4
    – Max S
    Sep 3 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.