Link Custom Block to custom page
Question
I created a custom block and was able to link to it on the home page or display it there. Unfortunately I don't get the block displayed on another page I created via the backend. If I understand the whole thing correctly, do I have to change the name of the .xml created in the layout? This is how it works:
and that's how it doesn't work:
Do you have any idea how I can get the second case working?
Solution
First you have to Know about how layout file name should make.
cms_index_index.xml
Here
cms is route name
index is controller name
index is controller action file name
Replace your required route and controller file.
Update
app/code/VendoreName/ModuleName/Controller/Index
Index.php
<?php
namespace VendoreName\ModuleName\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;
class Index extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
echo "hellow";
exit();
//return $this->resultPageFactory->create();
}
}
app/code/VendoreName/ModuleName/view/frontend/layout
summary_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="page.main.title">
<action method="setPageTitle">
<argument translate="true" name="title" xsi:type="string">Summary Page</argument>
</action>
</referenceBlock>
<referenceContainer name="content">
</referenceContainer>
</body>
</page>
app/code/VendoreName/ModuleName/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="summary" id="summary">
<module name="VendoreName_ModuleName"/>
</route>
</router>
</config>
Now run Magento command
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
php bin/magento c:f
and Clear your browser cache
url like : domain/summary
I Hope This Helps You.