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 :)