J
J
Johnny Show2021-02-26 16:14:51
PHP
Johnny Show, 2021-02-26 16:14:51

How to display text from module template?

It is not possible to display the text from the module template on the main page of the site for Magento2.4.2
Nothing is shown on the page.
Can you please tell me what I wrote wrong?
The module has registered and activated, the cache is disabled and cleared.
Module directories:
6038f28b6a8d7743009548.jpeg
In Block Helloworld.php folder

<?php
namespace Inchoo\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template
{
    public function getHelloWorldTxt()
    {
        return 'Hello world!';
    }
}

in Controller->Index index.php folders
<?php
 
namespace Inchoo\Helloworld\Controller\Index;
 
use Magento\Framework\App\Action\Context;
 
class Index extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;
 
    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
 
    public function execute()
    {
        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;
    }
}

routes.xml
<?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="helloworld" frontName="helloworld">
            <module name="Inchoo_Helloworld" />
        </route>
    </router>
</config>

module.xml
<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Inchoo_Helloworld" setup_version="1.0.0">
    </module>
</config>


helloworld.phtml
<h1><?=$this->getHelloWorldTxt(); ?></h1>
<div class="test_block">
 <p>Тестовый блок</p>
</div>

helloworld_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
    <body>
        <referenceContainer name="content">
            <block class="Inchoo\Helloworld\Block\Helloworld" name="helloworld" template="helloworld.phtml" />
        </referenceContainer>
    </body>
</page>

registration.php
<?php
 
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Inchoo_Helloworld',
    __DIR__
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
imdeveloper, 2021-02-26
@link_web

1)layout Files must be in frontend/layout folder. ref
2) You have created a custom router, but you want to display something on the main page. In order to display a block on the main page, you do not need to create a custom router - for this you need to create frontend/layout/cms_index_index.xml and add your block to the content. https://webkul.com/blog/how-to-show-a-block-content...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question