1

I'm trying to create a simple frontend module on Magento 2.3.2, It seems to work, but I see a blank content on frontend.

I have this folder structure

-app
    -code
        -MyVendor
            -MyModule
                -Block
                    --Index.php
                -Controller
                    -Index
                        --Index.php
                -etc
                    --module.xml
                    -frontend
                        --routes.xml
                -view
                    -frontend
                        -layout
                            --mymodule_index_index.xml
                        -templates
                            --homepage.phtml

These are the files

app/code/MyVendor/MyModule/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="MyVendor_MyModule" setup_version="1.0.0"/>
</config>

app/code/MyVendor/MyModule/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="myurl" id="myurl">
            <module name="MyVendor_MyModule"/>
        </route>
    </router>
</config>

app/code/MyVendor/MyModule/Controller/Index/Index.php

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

app/code/MyVendor/MyModule/Block/Index.php

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

    }
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

app/code/MyVendor/MyModule/view/frontend/layout/mymodule_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">
    <head>
        <title>My Title</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="MyVendor\MyModule\Block\Index" name="mymodule_index_index" template="MyVendor_MyModule::homepage.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/MyVendor/MyModule/view/frontend/templates/homepage.phtml

<h2>This is a custom module</h2>

Then I executed this commands

bin/magento setup:upgrade
bin/magento cache:flush
bin/magento cache:clean

Then I go to https://www.example.com/myurl/index/index I can see the theme, but no title and no "This is a custom module"

Any help would be appreciated :)

2 Answers 2

1

Change the layout from mymodule_index_index.xml to myurl_index_index.xml.

1
  • Thank you man! You are my life saver :D
    – fmineo
    Mar 30, 2020 at 10:45
1

the layout file will have name as format: {router_id}_{controller_name}_{action_name}.xml.

You can understand the layout in detail in this Magento topic , and the instruction of a layout structure.

When rendering page, Magento will check the layout file to find the handle for the page and then load Block and Template. We will create a layout handle file for this module:

Change the layout from mymodule_index_index.xml to myurl_index_index.xml.

for better understand please follow the below guide

https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.