1

I'm absolute newbie at magento2.

My code:

Block/Index.php

<?php
    namespace Vendor\Module\Block;
    class Index extends \Magento\Framework\View\Element\Template
    {

    }

Controller/Index.php

<?php
    namespace Vendor\Module\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();
        }
    }

Vendor/Module/etc/frontend/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 frontName="helloworld" id="helloworld">
        <module name="Vendor_Module"/>
    </route>
</router>
</config>

etc/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="Vendor_Module" setup_version="1.0.0">
</module>
</config>

Vendor/Module/view/frontend/template/index/index.phtml

<h2>Helloworld</h2>

Vendor/Module/view/layout/helloworld_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="Vendor\Module\Block\Index" name="index.index" template="Vendor_Module::index/index.phtml"/>
</referenceContainer>
</page>

Unfortunately

Helloworld

is not displaying. Any suggestions?

  • Can you send from which url are you trying to access page ? Also Vendor/Module/view/frontend/template/index.php file should be .phtml – user55548 Feb 18 at 11:47
  • 1
    Where is the layout.xml it should be helloworld_index_index.xml – Prathap Gunasekaran Feb 18 at 11:49
1

1/

Vendor/Module/view/frontend/templates/index/index.phtml

and not

Vendor/Module/view/frontend/template/index/index.phtml

note:

template in Magento 1

templates in Magento 2

2/

Vendor/Module/view/frontend/layout/helloworld_index_index.xml

and not

Vendor/Module/view/layout/helloworld_index_index.xml

1

I found 2 mistakes in the code

  1. Vendor/Module/view/layout/helloworld_index_index.xml this should be in Vendor/Module/view/frontend/layout/helloworld_index_index.xml

  2. Vendor/Module/view/frontend/template/index/index.phtml file this should be Vendor/Module/view/frontend/templates/index/index.phtml

Make above to changes and flush cache and then check

1

If you rename tha template file from index.php to index.phtml, move it to view/frontent/templates/index/ and add a layout xml file in view/frontend/layout/helloworld_index_index.xml your code should work.

For example if you just want to output the content of your template in the content area, you can use the following xml layout:

<?xml version="1.0" ?>
<page layout="1column" 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="Magento\Framework\View\Element\Template" name="index.index"  template="Vendor_Module::index/index.phtml"/>
        </referenceContainer>
    </body>
</page>

For using your bock you need to replace Magento\Framework\View\Element\Template with Vendor\Module\Block\Index in the layout xml.

  • I've done your suggested changes, but still no result and no errors as well... I have updated my code above. – Rufus Feb 18 at 12:42
  • please check if you have enabled layout caching, if yes you have to flush that cache. But it would be better to deactivate it – HelgeB Feb 18 at 12:55
  • There is a type in the template path, I updated my answer too - it must be "templates" not "template" – HelgeB Feb 18 at 13:59

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.