Magento2: Custom module not working
-
05-03-2021 - |
Frage
I have created new module but it is not working.Please Help me on this.
registration
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Outdoor_Configurator',
__DIR__
);
Module
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Outdoor_Configurator" setup_version="1.0.0">
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
<module name="Magento_Cms"/>
</sequence>
</module>
routes
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="testconfigurator" frontName="testconfigurator">
<module name="Outdoor_Configurator" />
</route>
</router>
Controller
<?php
namespace Vendor\ModuleName\Controller\Index;
class BuiltCatCollection extends \Magento\Framework\App\Action\Action
{
/** @var \Magento\Framework\View\Result\Page */
protected $resultPageFactory;
/** * @param \Magento\Framework\App\Action\Context $context */
public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Blog Index, shows a list of recent blog posts.
*
* @return \Magento\Framework\View\Result\PageFactory
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Custom Front View'));
return $resultPage;
}
}
Block
<?php
namespace Vendor\ModuleName\Block\Index;
class BuiltInfo extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Layout
<?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">
<body>
<referenceContainer name="content">
<block class="Vendor\ModuleName\Block\Index\BuiltInfo" name="outdoor_configurator" template="Outdoor_Configurator::builtcatcolinfo.phtml"></block>
</referenceContainer>
</body>
Template
<span style="font-weight: 400;">This is custom front view.</span>
Lösung
You should create your module like this.
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Outdoor_Configurator',
__DIR__
);
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Outdoor_Configurator" setup_version="1.0.0">
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
<module name="Magento_Cms"/>
</sequence>
</module>
</config>
etc/frontend/routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="configurator" frontName="configurator">
<module name="Outdoor_Configurator" />
</route>
</router>
</config>
Block/Index/Index.php
<?php
namespace Outdoor\Configurator\Block\Index;
class Index extends \Magento\Framework\View\Element\Template {
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = []) {
parent::__construct($context, $data);
}
protected function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Controller/Index/Index.php
<?php
namespace Outdoor\Configurator\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
view/frontend/templates/index/configurator_index_index.phtml
Hello Demo Page
view/frontend/layout/configurator_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<title>Titlename</title>
</head>
<body>
<referenceContainer name="sidebar.main">
<block class="Outdoor\Configurator\Block\Index\Sidebar" name="configurator_index_sidebar" before="-" template="Outdoor_Configurator::configurator_index_sidebar.phtml"/>
</referenceContainer>
<referenceContainer name="content">
<block class="Outdoor\Configurator\Block\Index\Index" name="configurator_index_index" template="Outdoor_Configurator::configurator_index_index.phtml"/>
</referenceContainer>
</body>
</page>
Please check Now I have updated all code, with file name also.
Andere Tipps
There are a few problems in your module:
You have used
Outdoor_Configurator
in module.xml but in Controller, Block, and layout you have usedVendor_Module
.registration.php
is missing from the code you have shared.You have not mentioned the file names and their paths. There may be a problem in the file naming convention.
Update:
Problems found in module shared at https://transfernow.net/813mi1m90ghk.
- The controller file name is Builtcatcollection.php.php, which is wrong. Notice .php twice.
- Code under block file is the same as in the controller file.
- Folder name
View
should beview
(small case). - The code under layout file is wrong.
Instead of:
<block class="Codism\Configurator\Block\Index\Builtcatcollection" name="index.builtcatcollection" template="Outdoor_Configurator::index/builtcatcollection.phtml"/>
it should be like:
<block class="Outdoor\Configurator\Block\Index\Builtcatcollection" name="index.builtcatcollection" template="Outdoor_Configurator::index/builtcatcollection.phtml"/>