Skip to main content
Rollback to Revision 4 - Edit approval overridden by post owner or moderator
Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121
how we use (use Prince\Helloworld\Model\HelloFactory;) where we defin that file??
Source Link
If need to get collection in template using block file
Source Link

If you want to get collection in template using block file

Another four files you need to add nad create

1. Block File

2. Layout File

3. Controller File

4. Templarte File

Create Block file Hello.php

namespace vendorName\ModuleName\Block\Index;

use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;

class Hello extends \Magento\Framework\View\Element\Template 
{
    protected $helloModelFactory;
    protected $request;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Hello $helloModelFactory,
        RequestInterface $request,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_helloModelFactory = $helloModelFactory;
        $this->_request = $request;
    }

    /**
     * Preparing global layout
     *
     * @return $this
     */
    protected function _prepareLayout() {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Display Collection'));
        return $this;
    }

    public function getHello()
    {
        $collection = $this->_helloModelFactory->getCollection();
        return $collection;
    }
}

Or Create Layout file routName_controllername_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
             <block class="vendorName\ModuleName\Block\Event\Index" name="newevent" template="vendorName_ModuleName::Index.phtml" />
        </referenceContainer>
    </body>
</page>

Create Controller File Index.php

<?php
namespace vendorName\ModuleName\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action 
{
    /**
     * @param Action\Context $context
     */

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute() {

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Create Template file Index.phtml

<?php

var_dump($block->getgetHello()->getData());

I hope it's work for ever

If you want to get collection in template using block file

Another four files you need to add nad create

1. Block File

2. Layout File

3. Controller File

4. Templarte File

Create Block file Hello.php

namespace vendorName\ModuleName\Block\Index;

use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;

class Hello extends \Magento\Framework\View\Element\Template 
{
    protected $helloModelFactory;
    protected $request;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Hello $helloModelFactory,
        RequestInterface $request,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_helloModelFactory = $helloModelFactory;
        $this->_request = $request;
    }

    /**
     * Preparing global layout
     *
     * @return $this
     */
    protected function _prepareLayout() {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Display Collection'));
        return $this;
    }

    public function getHello()
    {
        $collection = $this->_helloModelFactory->getCollection();
        return $collection;
    }
}

Or Create Layout file routName_controllername_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
             <block class="vendorName\ModuleName\Block\Event\Index" name="newevent" template="vendorName_ModuleName::Index.phtml" />
        </referenceContainer>
    </body>
</page>

Create Controller File Index.php

<?php
namespace vendorName\ModuleName\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action 
{
    /**
     * @param Action\Context $context
     */

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute() {

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Create Template file Index.phtml

<?php

var_dump($block->getgetHello()->getData());

I hope it's work for ever

added 40 characters in body
Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121
Loading
added 21 characters in body
Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121
Loading
Source Link
Prince Patel
  • 22.9k
  • 10
  • 98
  • 121
Loading