2

I am a new in Magento. I have installed Magento 2.2. and created 1 module with custom page. There is 1 block on this page, which has to be shown on it. But it is not shown. Please, help me to resolve this problem. I will write code and structure of module below.

Structure

enter image description here

Block/Helloworld.php

<?php
namespace Inchoo\Helloworld\Block;

class Helloworld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldTxt()
{
    return 'Hello world!';
}
}

Controller/Index/Index.php

<?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;
}

}

etc/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>

view/frontend/layout/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="Inchoo_Helloworld::helloworld.phtml" />
    </referenceContainer>
</body>

view/frontend/templates/helloworld.phtml

<h1><?php echo $this->getHelloWorldTxt(); ?></h1>

When I open page http://localhost/magento/helloworld/index/index, page is shown, but block with H1 and "Hello World!" is not shown. Thanks for a help.

2

I think that the problem is in your folder of template. Your screenshot shows the folder "veiw" instead of "view". You must rename it.

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.