Magento2: Custom block data not showing on frontend side
-
11-05-2021 - |
سؤال
I have created a simple module. But block data not display front end.
please find the below code
Block
<?php
namespace Codism\Csr\Block\Index;
class Additional_Csr extends \Magento\Framework\View\Element\Template
{
public function __construct(\Magento\Framework\View\Element\Template\Context $context)
{
parent::__construct($context);
}
public function showCustomView()
{
return __('Hello Magento 2');
}
}
Controller
<?php
namespace Codism\Csr\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();
}
}
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">
<referenceContainer name="content">
<block class="Codism\Csr\Block\Index\Additional_Csr" name="custom_view" template="Codism_Csr::additional_csr.phtml" />
</referenceContainer>
</page>
View
<span style="font-weight: 400;"><?php echo $block->showCustomView(); ?> </span>
Route
<?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="csr" frontName="csr">
<module name="Codism_Csr" />
</route>
</router>
</config>
المحلول
You need to change block file name :
It should be like this : /app/code/Codism/Csr/Block/Index/AdditionalCsr.php
<?php
namespace Codism\Csr\Block\Index;
class AdditionalCsr extends \Magento\Framework\View\Element\Template {
public function __construct(\Magento\Framework\View\Element\Template\Context $context) {
parent::__construct($context);
}
public function showCustomView() {
return __('Hello Magento 2');
}
}
Replace this below code in layout file : /app/code/Codism/Csr/view/frontend/layout/csr_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="Codism\Csr\Block\Index\AdditionalCsr" name="custom_view" template="Codism_Csr::additional_csr.phtml" />
</referenceContainer>
</page>
Clean cache and check output.
لا تنتمي إلى magento.stackexchange