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

  • 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');
    }
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.

  • Thansks for posting. If i hit the template file it shows only blank page for me. – Selva Kumar Nov 15 '16 at 13:48
  • i cant access $this->_modelPostFactory->create(); in block file – Selva Kumar 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

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.