0

I have created a simple module. But block data not display front end.

please find the below code

Block

<?php
namespace Codism\Csr\Block\Index;
class Additional_Csr extends \Magento\Framework\View\Element\Template
{
    public function __construct(\Magento\Framework\View\Element\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function showCustomView()
    {
        return __('Hello Magento 2');
    }
}

Controller

<?php
namespace Codism\Csr\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

Layout

<?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="Codism\Csr\Block\Index\Additional_Csr" name="custom_view" template="Codism_Csr::additional_csr.phtml" />
       </referenceContainer>
</page>

View

<span style="font-weight: 400;"><?php echo $block->showCustomView(); ?> </span>

Route

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="csr" frontName="csr">
            <module name="Codism_Csr" />
        </route>
    </router>
</config>
  • Can you please upload your routes.xml file and layout xml file name ? – Rohan Hapani Oct 24 '18 at 8:42
  • Please check route file @RohanHapani – Masud Shaikh Oct 24 '18 at 8:46
  • please check layout file name : csr_index_index.xml – Rohan Hapani Oct 24 '18 at 8:56
  • Ok, I will check and update you. – Masud Shaikh Oct 24 '18 at 9:04
  • check my answer. – Rohan Hapani Oct 24 '18 at 9:30
1

You need to change block file name :

It should be like this : /app/code/Codism/Csr/Block/Index/AdditionalCsr.php

<?php
namespace Codism\Csr\Block\Index;
class AdditionalCsr extends \Magento\Framework\View\Element\Template {
    public function __construct(\Magento\Framework\View\Element\Template\Context $context) {
        parent::__construct($context);
    }

    public function showCustomView() {
        return __('Hello Magento 2');
    }
}

Replace this below code in layout file : /app/code/Codism/Csr/view/frontend/layout/csr_index_index.xml

<?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="Codism\Csr\Block\Index\AdditionalCsr" name="custom_view" template="Codism_Csr::additional_csr.phtml" />
       </referenceContainer>
</page>

Clean cache and check output.

  • Can you explain what was the issue in my file? @RohanHapani – Masud Shaikh Oct 24 '18 at 10:48
  • 1
    Your file name was incorrect. – Rohan Hapani Oct 24 '18 at 10:55
  • Ok, xml file name also incorrect? – Masud Shaikh Oct 24 '18 at 10:56
  • Prv xml file name was additional_csr.xml. – Masud Shaikh Oct 24 '18 at 10:57
  • 1
    Yes. It should be csr_index_index.xml. I mention all file path in answer. – Rohan Hapani Oct 24 '18 at 11:07

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.